In this article, you'll learn about different ways to delete a directory in Java. We shall look at examples to delete both an empty and a non-empty directory structure.

Delete a directory using Files.delete()

In Java 7 and above, you can use Files.delete() from NIO API to easily delete an empty directory:

try {
    // directory path
    Path path = Paths.get("./tmp");

    // delete directory
    Files.delete(path);

} catch (IOException ex) {
    ex.printStackTrace();
}

The above code will throw an exception if the directory doesn't exist. To avoid the exception, you can use Files.deleteIfExists() that only deletes the directory if it exists:

try {
    // directory path
    Path path = Paths.get("./tmp");

    // delete directory
    Files.deleteIfExists(path);

} catch (IOException ex) {
    ex.printStackTrace();
}

Usually, we are required to delete a directory recursively. Both Files.delete() and Files.deleteIfExists() will throw an exception if the directory is not empty.

To delete a non-empty directory, we can use the Files.walk() method to list all files and sub-directories and then delete them one by one as shown below:

try {
    // create a stream
    Stream<Path> files = Files.walk(Paths.get("./tmp"));

    // delete directory including files and sub-folders
    files.sorted(Comparator.reverseOrder())
            .map(Path::toFile)
            .forEach(File::deleteOnExit);

    // close the stream
    files.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

Delete a directory using File.delete()

To delete an empty directory, we can also use the File.delete() method from the Java legacy I/O package:

// directory path
File file  = new File("./tmp");

// delete directory
file.delete();

If the directory is not empty, we have to do a little extra work and recursively delete all files and sub-folders as shown below:

public void deleteDir(File dir) {
    File[] files = dir.listFiles();
    if(files != null) {
        for (final File file : files) {
            deleteDir(file);
        }
    }
    dir.delete();
}

Now we can call the above function as follows:

File file = new File("./tmp");
deleteDir(file);

Delete a directory using Apache Commons IO

The Apache Commons IO library provides FileUtils.deleteDirectory() method to delete a directory including all files and sub-directories. Here is an example:

try {
    // directory path
    File file  = new File("./tmp");

    // delete directory
    FileUtils.deleteDirectory(file);

} catch (IOException ex) {
    ex.printStackTrace();
}

Don't forget to include Apache Commons IO dependency to your Maven's project pom.xml file:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

For a Gradle project, add the following dependency to your build.gradle file:

implementation 'commons-io:commons-io:2.6'

Further Reading

You may be interested in other Java I/O articles:

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.