Deleting a folder with 1 million files on Windows Server 2008

adrian78 used Ask the Experts™
I am trying to delete a folder that has about 1 million files in it.  It is a Windows Server 2008.  It is a temporary folder containing small XML files that were I guess downloaded when a SQL Server Job was being executed that imported data from an XML Feed.

The problem is that I try deleting the files via command prompt and it just doesn't work.  I think it ends up hanging due to enumerating the files.  I am not sure how to delete all of the files without enumerating.

Any help would be appreciated!
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Scott SilvaNetwork Administrator
Have you tried (from an administrative cmd prompt);

rd "directory-path" /S /Q

Open in new window

This should kill the directory along with all subdirectories and files.
Lead SaaS Infrastructure Engineer
Using RMDir (As mentioned above by Scott Silva) if far and away the FASTEST possible method for deleting all of the files.

I have gone through many experiences where I need to be rid of many files and removing the entire directory is always preferable if possible.

You may still need to let it run for an hour or two depending on your disk's performance characteristics, and RD won't write a status (which is far faster than if it did)

However, I have come up with a neat little for-loop that allows me to keep a watch on the directory for number of files to get a good watch status that does not seem to affect the speed of the over-all delete.

So Open Two Administrative CMD Prompts.

In the first one, you can Run an RD (and assuming you also need to keep this directory around just chain it with an MD at the end) as so:

RD /S /Q "C:\Path\To\Files" && MD "C:\Path\To\Files"

Open in new window

Hit enter and let that run.

Move immediately to the second CMD window and enter this code to monitor the file delete's progress:

FOR /L %L IN (1,1,32000) DO @(echo.|Set /p "=%~L - "&dir "C:\Path\To\Files" | FIND /I "File(s)"&Ping -n 1 -w 1000>NUL)

Open in new window

Hit enter and let that run.

Now you'll get a nice self-updating status of the file count once per second (with a counter of the total number of seconds elapsed) without a lot of extra overhead. :)
Distinguished Expert 2018

one other option could be open folder within WinRAR and delete from WinRAR itself, you do need to install WinRAR trial version on server

I have used WinRAR to delete folders which contains files with more than 220 characters long


Thanks guys!  The hosts manage to do it for me just before your solution was posted but I'll be using it for future.  Appreciate it.
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Glad to Help Adrian :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial