How to delete directory content in Java? [duplicate]
Asked Answered
T

8

102

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.

Tull answered 14/10, 2011 at 13:8 Comment(1)
Does it even compile? you are calling delete on array.Danille
S
139

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);
Sinapism answered 14/10, 2011 at 13:10 Comment(7)
+1. However, the last line should be outputFolder.delete() instead of output.delete().Startling
I tried it but when I open browser, folder is still here and contains all files...Tull
Wont work if one of the files is a non empty directory. You have to delete directory contents recursively. if(f.isDirectory())myDelete(f)Alaric
Your're right, changed the codeSinapism
I got nullPointerExcpetion on for(File f: files) {Tull
Fixed it: download.oracle.com/javase/7/docs/api/java/io/… sometimes returns nullSinapism
This deletes folder too, which is not wanted in the question. To not delete folder, move folder.delete() to the end of if(f.isDirectory()) {..} block.Macaw
W
114

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.

Watershed answered 26/12, 2011 at 4:24 Comment(5)
i guess you mean the commons-io method: org.apache.commons.io.FileUtils.deleteDirectory(File)Laurencelaurene
Almost there! The question was how to delete the folder's content (not the filter itself) - commons-io has a method for that as well: FileUtils.cleanDirectory(File);Novikoff
Is there a similar way to delete only the files inside the folder and not the folder itself??Retha
@Retha check the comment above. You can use FileUtils.cleanDirectory(File) method as Daniel has mentionedBrandonbrandt
I'm using org.apache.commons.io.FileUtils.deleteDirectory(File) but still get "IOException: Unable to delete directory" with no other information. The application I am running (locally on MacOS) creates the directory and clones a git repo into it. When I restart the application, FileUtils.deleteDirectory(File) is called to reset things and that's when I get the error. The same app that creates the directory cannot delete the directory upon a second startup. It always runs under my user, which has full access to that directory. What am I missing? :)Synagogue
J
11

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:

Jodhpur answered 14/10, 2011 at 13:12 Comment(2)
Unfortunately, Files.deleteRecursively() was removed from Guava in version 11.0.Hoskins
FileUtils.forceDelete() belongs to commons-io.Carloscarlota
C
8

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...

Cleary answered 14/10, 2011 at 13:12 Comment(0)
H
5

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();
}
Haemostatic answered 3/7, 2013 at 10:25 Comment(1)
This code block: "if (!delete(f)) { return false; }" could be replaced with "return delete(f);" could it not?Gdynia
S
4

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();
Synchro answered 14/10, 2011 at 13:16 Comment(0)
C
2

Use FileUtils with FileUtils.deleteDirectory();

Conwell answered 31/1, 2013 at 14:42 Comment(1)
Better mention that FileUtils is a part of Apache Commons IO.Worser
M
0
for(File f : files) {
    f.delete();
}    
files.delete(); // will work
Makowski answered 14/10, 2011 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.