Java tutorial
//package com.java2s; //License from project: Apache License import java.io.*; public class Main { /** * Try to delete directory in a fast way. */ 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(); } } /** * recursively delete * * @param dir * @throws java.io.IOException */ public static void deleteDirectoryRecursively(File dir) throws IOException { File[] files = dir.listFiles(); if (files == null) { throw new IllegalArgumentException("not a directory: " + dir); } for (File file : files) { if (file.isDirectory()) { deleteDirectoryRecursively(file); } if (!file.delete()) { throw new IOException("failed to delete file: " + file); } } } }