Is there a way to delete entire directories recursively in Java?
In the normal case it is possible to delete an empty directory. However when it comes to deleting entire directories with contents, it is not that simple anymore.
How do you delete entire directories with contents in Java?
Below code recursively delete all contents in a given folder.
With Java 7, we can finally do this with reliable symlink detection. (I don't consider Apache's commons-io to have reliable symlink detection at this time, as it doesn't handle links on Windows created with
mklink
.)For the sake of history, here's a pre-Java 7 answer, which follows symlinks.
Here is a bare bones main method that accepts a command line argument, you may need to append your own error checking or mold it to how you see fit.
I hope that helps!
A solution with a stack and without recursive methods:
Well, let's assume an example,
For more information refer below resources
Delete directory
Just saw my solution is more or less the same as erickson's, just packaged as a static method. Drop this somewhere, it's much lighter weight than installing all of Apache Commons for something that (as you can see) is quite simple.