How to delete non empty directory in java


In this post, we will see how to delete┬áDirectory/Folder which is non empty. You can use ‘s delete empy folder but you can not delete it if it is non empty.
There are multiple ways to do it.

  • Using java recursion
  • Using Apache common IO

Using java recursion:

It is very straight forward to do it. It will iterate through all files in the folder. If it is a file then we can directly delete it but if It finds folder inside it then we again call same method.

Using Apache common IO:

Delete non empty folder is very straight forward. You just need to call FileUtils.deleteDirectory() method.

Java Program:

When I ran above program, I got following output


Add Comment