Recursively delete a file and all its contents : Delete « File Input Output « Java






Recursively delete a file and all its contents

 
import java.io.File;

public class Utils {
  /**
   * Recursively delete a file and all its contents.
   * 
   * @param root
   *          the root to delete
   */
  public static void recursiveDelete(File root) {
    if (root == null) {
      return;
    }

    if (root.isDirectory()) {
      File[] files = root.listFiles();
      if (files != null) {
        for (int i = 0; i < files.length; i++) {
          File file = files[i];
          if (file.isDirectory()) {
            recursiveDelete(file);
          } else {
            file.delete();
          }
        }
      }
    }
    root.delete();
  }
}

   
  








Related examples in the same category

1.Deletes all files and subdirectories
2.Remove file or directory
3.Utilities for file delete copy close
4.Deletes the diretory and any files and directories in it recursively.
5.Delete Recursively
6.Delete a file
7.Delete all files under this file and including this file
8.Delete the file or non-empty directory at the supplied path
9.Deletes a directory.
10.Empty and delete a folder (and subfolders).