public static void deleteDirectoryQuickly ( File dir ) throws IOException { if ( ! dir . exists ( ) ) { return ; } final File to = new File ( dir . getAbsolutePath ( ) + System . currentTimeMillis ( ) ) ; dir . renameTo ( to ) ; if ( ! dir . exists ( ) ) { // rebuild dir . mkdirs ( ) ; } // try to run "rm -r" to remove the whole directory if ( to . exists ( ) ) { String deleteCmd = "rm -r " + to ; Runtime runtime = Runtime . getRuntime ( ) ; try { Process process = runtime . exec ( deleteCmd ) ; process . waitFor ( ) ; } catch ( IOException e ) { } catch ( InterruptedException e ) { e . printStackTrace ( ) ; } } if ( ! to . exists ( ) ) { return ; } deleteDirectoryRecursively ( to ) ; if ( to . exists ( ) ) { to . delete ( ) ; } }
Try to delete directory in a fast way .