99

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
lola
  • 5,649
  • 11
  • 49
  • 61

8 Answers8

136

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);
NCode
  • 2,758
  • 1
  • 16
  • 25
  • +1. However, the last line should be `outputFolder.delete()` instead of `output.delete()`. – spork Oct 14 '11 at 13:16
  • I tried it but when I open browser, folder is still here and contains all files... – lola Oct 14 '11 at 13:18
  • 3
    Wont work if one of the files is a non empty directory. You have to delete directory contents recursively. if(f.isDirectory())myDelete(f) – josefx Oct 14 '11 at 13:20
  • Your're right, changed the code – NCode Oct 14 '11 at 13:37
  • I got nullPointerExcpetion on for(File f: files) { – lola Oct 14 '11 at 13:44
  • Fixed it: http://download.oracle.com/javase/7/docs/api/java/io/File.html#listFiles%28%29 sometimes returns null – NCode Oct 14 '11 at 13:51
  • 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. – palindrom Jan 31 '17 at 12:48
110

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Dhruv
  • 10,291
  • 18
  • 77
  • 126
  • 19
    i guess you mean the commons-io method: org.apache.commons.io.FileUtils.deleteDirectory(File) – cproinger Sep 11 '12 at 09:34
  • 75
    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); – Daniel Mar 23 '13 at 22:09
  • Is there a similar way to delete only the files inside the folder and not the folder itself?? – Lucy Nov 03 '14 at 06:27
  • @Lucy check the comment above. You can use FileUtils.cleanDirectory(File) method as Daniel has mentioned – vnkid Nov 09 '18 at 08:21
  • 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? :) – Jack Straw Mar 16 '23 at 00:46
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:

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 10
    Unfortunately, [`Files.deleteRecursively()` was removed from Guava](http://stackoverflow.com/questions/8320376/why-is-files-deletedirectorycontents-deprecated-in-guava) in version 11.0. – Jonik Jan 13 '14 at 10:11
  • 1
    FileUtils.forceDelete() belongs to commons-io. – niaomingjian Jul 19 '18 at 09:36
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...

solendil
  • 8,432
  • 4
  • 28
  • 29
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();
}
Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
user2546090
  • 59
  • 1
  • 1
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();
fireshadow52
  • 6,298
  • 2
  • 30
  • 46
2

Use FileUtils with FileUtils.deleteDirectory();

Dellanio
  • 309
  • 3
  • 4
0
for(File f : files) {
    f.delete();
}    
files.delete(); // will work
Vaandu
  • 4,857
  • 12
  • 49
  • 75