Java Utililty Methods Delete File Recursively

List of utility methods to do Delete File Recursively

Description

The list of methods to do Delete File Recursively are organized into topic(s).

Method

voiddeleteRecursiveDirectories(String mainDir)
delete Recursive Directories
File dir = new File(mainDir);
File subDir1 = new File(dir.getAbsolutePath() + "\\subdir1");
File subDir2 = new File(dir.getAbsolutePath() + "\\subdir2");
File tempFile1 = new File(dir.getAbsolutePath() + "\\file1.txt");
File tempFile2 = new File(subDir1.getAbsolutePath() + "\\file2.txt");
tempFile1.delete();
tempFile2.delete();
subDir1.delete();
...
voiddeleteRecursiveDirectory(String dirName)
clear out all files recursively in a directory, including the directory itself
File dir = new File(dirName);
if (!dir.exists()) {
    return;
if (!dir.isDirectory()) {
    throw new RuntimeException("The directory: " + dirName + " is not a directory");
File[] allFiles = dir.listFiles();
...
booleandeleteRecursiveFileDir(File dir)
Delete the directory and its subdirs associated with the File dir if empty
if (dir == null) {
    return true;
boolean retour = true;
if (!dir.exists()) {
    return true;
File[] list = dir.listFiles();
...
booleandeleteRecursively(File dir)
Attempts to recursively delete a directory.
if (dir == null) {
    return false;
Deque<File> stack = new ArrayDeque<File>();
stack.push(dir);
boolean result = true;
while (!stack.isEmpty()) {
    File topElement = stack.peek();
...
voiddeleteRecursively(File directory)
Recursively delete directories and files.
if (directory == null) {
    throw new IllegalArgumentException("Directory may not be null.");
} else if (directory.listFiles() == null) {
    throw new IllegalArgumentException(directory.getAbsolutePath() + " is not a valid directory.");
for (File entry : directory.listFiles()) {
    if (entry.isDirectory()) {
        deleteRecursively(entry);
...
voiddeleteRecursively(File dirEntry)
Deletes a file or directory (recursively).
checkNotNull(dirEntry, "null directory entry");
if (!dirEntry.exists()) {
    return;
if (dirEntry.isDirectory()) {
    File[] files = dirEntry.listFiles();
    if (files != null) {
        for (File file : files) {
...
voiddeleteRecursively(File f)
delete Recursively
if (f.isDirectory()) {
    for (String s : f.list()) {
        deleteRecursively(new File(f, s));
boolean b = f.delete();
if (!b) {
    throw new IOException("failed to delete " + f);
...
voiddeleteRecursively(File f)
delete Recursively
if (f.isDirectory()) {
    for (File child : f.listFiles()) {
        deleteRecursively(child);
if (!f.delete()) {
    System.err.println("Deleting file " + f.getAbsolutePath() + " failed!");
voiddeleteRecursively(File file)
Delete a file or a directory recursively.
if (file.isDirectory()) {
    File[] children = file.listFiles();
    if (children != null) {
        for (File child : children) {
            deleteRecursively(child);
if (!file.delete()) {
    Uninterruptibles.sleepUninterruptibly(5, TimeUnit.SECONDS);
    if (!file.delete()) {
        throw new IOException("Could not delete: " + file);
voiddeleteRecursively(File file)
delete Recursively
if (file.isDirectory()) {
    deleteDirectoryContents(file);
if (!file.delete()) {
    throw new IOException("Failed to delete " + file);