The File class represents files and directories and provides methods to perform file-system operations. The delete() method can be used to delete a file or an empty directory. To delete a non-empty directory, its files and subdirectories must first be deleted. This can be done recursively by traversing the directory structure.
- The delete() method can delete files and empty directories.
- A non-empty directory cannot be deleted directly using delete().
- Apache Commons IO providesFileUtils.deleteDirectory() for recursively deleting a directory and its contents.
Examples
Input: file = "C:\JavaFolderTest\file1.txt"
Output: File deleted successfullyInput: directory = "C:\JavaFolderTest\EmptyFolder"
Output: Directory deleted successfullyInput: directory = "C:\JavaFolderTest" containing files and subdirectories
Output: Directory and its contents deleted successfully
Suppose there exists a directory with path C:\\GFG. The following image displays the files and directories present inside GFG folder. The subdirectory Ritik contains a file named Logistics.xlsx and subdirectory Rohan contains a file named Payments.xlsx.

Methods To Delete The Directory
Java provides different approaches to delete a directory depending on whether it is empty or contains files and subdirectories.
Method 1: using delete() to delete files and empty folders
For a non-empty directory, we first need to delete all the files and subdirectories inside it. A recursive method can be used to traverse the directory and delete its contents.
Approach
- Provide the path of the directory.
- Create a File object for the directory.
- Use listFiles() to access the files and subdirectories.
- Recursively delete the contents of each subdirectory.
- Delete the files and empty subdirectories using delete().
- Finally, delete the main directory.
import java.io.File;
class DeleteDirectory {
// Method to recursively delete files and subdirectories
public static void deleteDirectory(File file)
{
File[] files = file.listFiles();
// Check whether the directory can be accessed
if (files == null) {
return;
}
// Traverse all files and subdirectories
for (File subfile : files) {
// If it is a subdirectory, recursively delete
// its contents
if (subfile.isDirectory()) {
deleteDirectory(subfile);
}
// Delete the file or empty directory
subfile.delete();
}
}
public static void main(String[] args)
{
// Store the directory path
String filepath = "C:\\GFG";
File file = new File(filepath);
// Check whether the directory exists
if (!file.exists()) {
System.out.println("Directory does not exist.");
return;
}
// Delete files and subdirectories
deleteDirectory(file);
// Delete the main directory
boolean deleted = file.delete();
if (deleted) {
System.out.println(
"Directory deleted successfully.");
}
else {
System.out.println(
"Failed to delete the directory.");
}
}
}
Output
Directory deleted successfully.Following is the image of C drive where no GFG folder is present.

Explanation: The deleteDirectory() method traverses all files and subdirectories using listFiles(). If a subdirectory is found, the method recursively calls itself to delete its contents. After the contents are removed, the files and empty directories are deleted using delete().
Method 2: using deleteDirectory() method from commons-io
Apache Commons IO provides the FileUtils.deleteDirectory() method to delete a directory recursively. It can remove the directory along with all its files and subdirectories. To use deleteDirectory() method you need to add a commons-io dependency to maven project.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
import java.io.File;
import org.apache.commons.io.FileUtils;
class DeleteDirectory {
public static void main(String[] args)
{
// Store the directory path
String filepath = "C:\\GFG";
File file = new File(filepath);
// Check whether the directory exists
if (!file.exists()) {
System.out.println("Directory does not exist.");
return;
}
try {
// Delete the directory and its contents
// recursively
FileUtils.deleteDirectory(file);
System.out.println(
"Directory deleted successfully.");
}
catch (Exception e) {
System.out.println(
"Failed to delete the directory.");
e.printStackTrace();
}
}
}
Output
Directory deleted successfully.Following is the image of C drive where no GFG folder is present.

Explanation: The program creates a File object for the GFG directory and passes it to FileUtils.deleteDirectory(). This method recursively deletes the files and subdirectories before removing the specified directory.