Let’s learn how to delete a directory in java.
How to delete a directory in java
To delete a directory use delete() method of File class. delete() method deletes file or directory denoted by this abstract pathname.
If this pathname denotes a directory, then the directory must be empty in order to be deleted.

delete() method returns true if and only if the file or directory is successfully deleted; false otherwise.
In the below image you can see directory path B:\HelloWorld which displays directories and files inside HelloWorld folder.

Now let’s see how to delete a directory using recursion or recursively.
import java.io.File; import java.io.IOException; public class DeleteDirectoryExample { public static void main(String[] args) throws IOException { String strPath = "B:\\HelloWorld"; File fl = new File(strPath); deleteDirectoryRecursively(fl); // deleting main folder fl.delete(); } public static void deleteDirectoryRecursively(File fl) { for(File subfile : fl.listFiles()) { // recursively calling function to empty sub folder if(subfile.isDirectory()) { deleteDirectoryRecursively(subfile); } // deleting files and emptying sub folders subfile.delete(); } } }
Output:
As you can see in the below image there is no HelloWorld folder.

Also read – java overview