How To Recursively Delete Files And Directories Using Java

By Angsuman Chakraborty, Gaea News Network
Thursday, June 15, 2006

How can you recursively delete files and directories in windows? You can select them in explorer (in windows) and delete it, right?

The problem with the explorer approach is that if one or more files and / or directories cannot be deleted then the whole process fails. Try cleaning your Local Settings\temp directory to see an example. Most likely it will fail in windows explorer.

Also I have seen it fail when large number of files are selected. It is slow and not usable from command line.

(In Unix/linux the problem is much simpler - rm -rf *)

However for the moment let’s see how we can simplify this on windows using a bit of java. I present with with a superfast method to delete multiple files and directories from command line recursively.

Download Delete.class

Usage:
java Delete [list of files and / or directories]

No warranties (express or implied) are provided. Use at your own risk. All deletion using this utility is final. It cannot be recovered from Recycle Bin (on windows).

Discussion
August 16, 2009: 10:04 pm

Hi! I agree with SR… great utility class that’s gotten me out of a bind over the last three years more than a few times. Usually it takes a few seconds to rip through a bunch of nested directories (darn those sploggers) but today it’s been running for 20+ minutes. Moderate CPU usage though (~25%) so I’ll let it go for a while. My question: since I don’t have access to the code (nor do I need it… would be too tempting to make a change, ultimately hurting myself/wiping hard drive/etc.) is your sense that it’s doing good delete work (and that I just had a ton of nested dirs) or are there some points where it waits for something (file locks, etc)? Appreciate any feedback although I do realize you probably haven’t looked at or thought about this class in a few years. Take care, Joe


a_kitten
January 10, 2009: 3:43 am

Bad chinese translate.
I can not understand it~~~(T_T)

October 29, 2008: 1:06 am

I am happy to hear that. Hope to see you more on our blog.

Take care,
Angsuman


SR
October 28, 2008: 10:18 pm

I have tried all possible DOS commands to delete the files recursively. These files were created in a loop by WSAD and there might be around 200-250 subdirectories. The file names & paths were too long to do anything. I crossed my fingers and tried the Delete class. It hardly took 2-3 seconds..

Thank You (and God)! I finally got rid of these unnecessary files.

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :