# How to delete a directory in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Dec 13, 2019, updated Oct 14, 2022. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-delete-directory

> A quick article to learn how to delete an empty as well as non-empty directory in Java.

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](https://attacomsian.com/blog/java-delete-directory-recursively).

## 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:

```java
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:

```java
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](https://attacomsian.com/blog/java-list-all-files-in-a-directory) and sub-directories and then delete them one by one as shown below:

```java
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:

```java
// 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:

```java
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:

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


## Delete a directory using Apache Commons IO

The [Apache Commons IO](https://commons.apache.org/proper/commons-io/) library provides `FileUtils.deleteDirectory()` method to delete a directory including all files and sub-directories. Here is an example:

```java
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:

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

For a [Gradle project](https://attacomsian.com/blog/install-gradle-on-ubuntu), add the following dependency to your `build.gradle` file:

```groovy
implementation 'commons-io:commons-io:2.6'
```

## Further Reading

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

* [Reading and Writing Files in Java](https://attacomsian.com/blog/java-read-write-files)
* [How to Read and Write Text Files in Java](https://attacomsian.com/blog/java-read-write-text-files)
* [How to Read and Write Binary Files in Java](https://attacomsian.com/blog/java-read-write-binary-files)
* [Reading and Writing Files using Java NIO API](https://attacomsian.com/blog/java-read-write-files-nio-api)
* [How to read a file line by line in Java](https://attacomsian.com/blog/how-to-read-a-file-line-by-line-in-java)
