How to Delete Directory in Java

In this post we will see How to Delete Directory in Java. We will use the File.delete() method. But to use this method we need the directory to be empty.
I have an directory “kscodes” and its has following folders and files inside it.
How to Delete Directory in Java
Lets try to delete the directory “kscodes” using File.delete()

Output

This happened because the directory “kscodes” is not empty.
So to delete any directory using File.delete() we first need to empty the directory.

Lets see an example that recursively deletes all the contents from the kscodes directory and then deletes the directory “kscodes” at the end.

Output

Reference

File delete()

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn