Mass deleting files in Windows
I have a directory, that contains ~ 3 million files in certain subdirectories on a Windows 2008 server. Manually deleting the files via SHIFT+DEL on the root dir takes ages. Is there any other way to do the deletion in a faster manner?
Solution 1:
WARNING: if you have symlinks to directories then del
will delete actual directories and not symlinks. Be very careful with this and do not run these commands unless you know there are no symlinks inside target directory.
I regularly need to delete lots of files and directories from a WinXP encrypted drive, typically around 22 GB of 500,000 files in 45,000 folders.
Deleting with Windows Explorer is rubbish because it wastes lots of time enumerating the files. I usually move the stuff I need to delete to C:\stufftodelete and have a deletestuff.bat batch file to rmdir /s/q C:\stufftodelete
. This is scheduled to run at night, but sometimes I need to run it during the day so the quicker the better.
Here's the results of a quick time test of a small 5.85 MB sample of 960 files in 303 folders. I ran method 1 followed by method 2, then reset the test directories.
Method 1 removes the files and directory structure in one pass:
rmdir /s/q foldername
Method 2 has a first pass to delete files and outputs to nul to avoid the overhead of writing to screen for every singe file. A second pass then cleans up the remaining directory structure:
del /f/s/q foldername > nul
rmdir /s/q foldername
- Method 1: 17.5s, 14.9s, 13.9s, 14.8s, 13.8s: average 14.98 seconds
- Method 2: 14.3s, 12.1s, 11.7s, 14.2s, 11.8s: average 12.82 seconds
Here's results of another test using 404 MB of 19,521 files in 3,243 folders:
- Method 1: 2 minutes 20 seconds
- Method 2: 2 minutes 33 seconds
So there's not much in it, probably too close to judge on a single test.
Edit: I've retested with much more data, this is a typical case for me: 28.3 GB of 1,159,211 files in 146,918 folders:
- Method 1: 2h 15m, 2h 34m: average: 2 hours 25 minutes
- Method 2: 49m, 57m: average: 53 minutes
Wow, method 2 is nearly three times faster than method 1! I'll be updating my deletestuff.bat!
Solution 2:
If you have to delete large directory trees regularly, consider storing the root of that directory tree on a separate partition, then simply quick-format it whenever you need to delete everything. If you need to automate this, you can use this DOS command:
echo Y | format Z: /FS:NTFS /X /Q
where Z: is your 'volatile' partition. Note: the partition must have no label. I blogged about this here.