How to delete directory content in Java?

JavaFile IoDelete Directory

Java Problem Overview


After enumerating a directory, I now need to delete all the files.

I used:

final File[] files = outputFolder.listFiles();
files.delete();

But this hasn't deleted the directory.

Java Solutions


Solution 1 - Java

You have to do this for each File:

public static void deleteFolder(File folder) {
    File[] files = folder.listFiles();
    if(files!=null) { //some JVMs return null for empty dirs
        for(File f: files) {
            if(f.isDirectory()) {
                deleteFolder(f);
            } else {
                f.delete();
            }
        }
    }
    folder.delete();
}

Then call

deleteFolder(outputFolder);

Solution 2 - Java

To delete folder having files, no need of loops or recursive search. You can directly use:

FileUtils.deleteDirectory(<File object of directory>);

This function will directory delete the folder and all files in it.

Solution 3 - Java

All files must be delete from the directory before it is deleted.

There are third party libraries that have a lot of common utilities, including ones that does that for you:

Solution 4 - Java

You can't delete on an array ! This should work better :

for (File f : files) f.delete();

But it won't work if the folders are not empty. For this cases, you will need to recursively descend into the folder hierarchy and delete everything. Yes it's a shame Java can't do that by default...

Solution 5 - Java

Here is one possible solution to solve the problem without a library :

public static boolean delete(File file) {

    File[] flist = null;
    
    if(file == null){
        return false;
    }
    
    if (file.isFile()) {
        return file.delete();
    }
    
    if (!file.isDirectory()) {
        return false;
    }
    
    flist = file.listFiles();
    if (flist != null && flist.length > 0) {
        for (File f : flist) {
            if (!delete(f)) {
                return false;
            }
        }
    }
    
    return file.delete();
}

Solution 6 - Java

You can't delete an File array. As all of the other answers suggest, you must delete each individual file before deleting the folder...

final File[] files = outputFolder.listFiles();
for (File f: files) f.delete();
outputFolder.delete();

Solution 7 - Java

Use FileUtils with FileUtils.deleteDirectory();

Solution 8 - Java

for(File f : files) {
    f.delete();
}    
files.delete(); // will work

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlolaView Question on Stackoverflow
Solution 1 - JavaNCodeView Answer on Stackoverflow
Solution 2 - JavaDhruvView Answer on Stackoverflow
Solution 3 - JavaBozhoView Answer on Stackoverflow
Solution 4 - JavasolendilView Answer on Stackoverflow
Solution 5 - Javauser2546090View Answer on Stackoverflow
Solution 6 - Javafireshadow52View Answer on Stackoverflow
Solution 7 - JavaDellanioView Answer on Stackoverflow
Solution 8 - JavaVaanduView Answer on Stackoverflow