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

Using Files.delete() Method

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() which 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 require 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 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();
}

Using File.delete() Method

To delete an empty directory, we can also use the File.delete() method from 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);

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.gralde file:

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

Further Reading

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

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Subscribe to RSS Feed.

👋 If you enjoy reading my articles and want to support me to continue creating free tutorials, ☕ Buy me a coffee (cost $5).