public static boolean deleteDirectory(String directory,boolean useOSNativeDelete) throws IOException {
  boolean result=false;
  if (!useOSNativeDelete) {
    File dir=new File(directory);
    for (    File file : dir.listFiles()) {
      if (file.delete()) {
        log.debug("{} was deleted",file.getName());
      }
 else {
        log.debug("{} was not deleted",file.getName());
        file.deleteOnExit();
      }
      file=null;
    }
    if (dir.delete()) {
      log.debug("Directory was deleted");
      result=true;
    }
 else {
      log.debug("Directory was not deleted, it may be deleted on exit");
      dir.deleteOnExit();
    }
    dir=null;
  }
 else {
    Process p=null;
    Thread std=null;
    try {
      Runtime runTime=Runtime.getRuntime();
      log.debug("Execute runtime");
      if (File.separatorChar == '\\') {
        p=runTime.exec("CMD /D /C \"RMDIR /Q /S " + directory.replace('/','\\') + "\"");
      }
 else {
        p=runTime.exec("rm -rf " + directory.replace('\\',File.separatorChar));
      }
      std=stdOut(p);
      while (std.isAlive()) {
        try {
          Thread.sleep(250);
        }
 catch (        Exception e) {
        }
      }
      log.debug("Process threads wait exited");
      result=true;
    }
 catch (    Exception e) {
      log.error("Error running delete script",e);
    }
 finally {
      if (null != p) {
        log.debug("Destroying process");
        p.destroy();
        p=null;
      }
      std=null;
    }
  }
  return result;
}

public static boolean deleteDirectory ( String directory , boolean useOSNativeDelete ) throws IOException { boolean result = false ; if ( ! useOSNativeDelete ) { File dir = new File ( directory ) ; for ( File file : dir . listFiles ( ) ) { if ( file . delete ( ) ) { log . debug ( "{} was deleted" , file . getName ( ) ) ; } else { log . debug ( "{} was not deleted" , file . getName ( ) ) ; file . deleteOnExit ( ) ; } file = null ; } if ( dir . delete ( ) ) { log . debug ( "Directory was deleted" ) ; result = true ; } else { log . debug ( "Directory was not deleted, it may be deleted on exit" ) ; dir . deleteOnExit ( ) ; } dir = null ; } else { Process p = null ; Thread std = null ; try { Runtime runTime = Runtime . getRuntime ( ) ; log . debug ( "Execute runtime" ) ; if ( File . separatorChar == '\\' ) { p = runTime . exec ( "CMD /D /C \"RMDIR /Q /S " + directory . replace ( '/' , '\\' ) + "\"" ) ; } else { p = runTime . exec ( "rm -rf " + directory . replace ( '\\' , File . separatorChar ) ) ; } std = stdOut ( p ) ; while ( std . isAlive ( ) ) { try { Thread . sleep ( 250 ) ; } catch ( Exception e ) { } } log . debug ( "Process threads wait exited" ) ; result = true ; } catch ( Exception e ) { log . error ( "Error running delete script" , e ) ; } finally { if ( null != p ) { log . debug ( "Destroying process" ) ; p . destroy ( ) ; p = null ; } std = null ; } } return result ; }
deletes a directory and its contents . this will fail if there are any file locks or if the directory cannot be emptied .
