156

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?

Cfinley
  • 1,435

14 Answers14

198

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!

Hugo
  • 3,010
19

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.

10

In command prompt (Start -> Run -> cmd):

del /f /s /q foldername 
amiregelz
  • 8,297
lmsasu
  • 283
7

In addition to copying/moving files very fast (using its own API), TeraCopy can delete files and it's very fast too. Ever since finding out TeraCopy I don't use a computer without it installed (if I'm gonna copy/move/delete).

The same installer installs x64 edition but to use it you have to manually force it.

The beta which I recommend over the stable versions: http://blog.codesector.com/2010/09/22/teracopy-2-2-beta-3/

6

Using the code below usually works well for me.

mkdir empty_dir
robocopy empty_dir dir_to_wipe /mir /r:0 /w:0 /e
rmdir empty_dir dir_to_wipe
wizlog
  • 13,573
Benoit
  • 7,113
6

I did a bat file that do the same.

@echo off
echo --------------------WARNING--------------------
echo folder "%~1" will be deleted
echo --------------------WARNING--------------------
pause
echo Deleting folder: "%~1".
time /T
del /f/s/q "%~1" >nul
rmdir /s/q "%~1" >nul
echo Done.
time /T
echo --- Taking ownership.
takeown /f "%~1" /r /d y >nul
icacls "%~1" /reset /t >nul
icacls "%~1" /setowner "%username%" /t >nul
echo Done all.
time /T

it do the work in two step, one, it try to delete the files. And the second is to try to take ownership of the files. y should be changed according your local (stand for yes in english). If fail the task (for example if fail because permission) then you must run it again. However, the second round will not try to delete the files deleted in the first round, so it could be pretty quick.


How to use it. Save as delfolder.bat in a path route (for example c:\windows), then run it as

 delfolder "foldername"   

where foldername is the name of the folder

In my test, deleting 123'000 files took 3 minutes (sata 7200rpm). YAY!

Benoit
  • 7,113
3

Use the rd /s command from the command prompt.

JP Alioto
  • 6,550
2

The best practical solution is probably to move the folder out of the way somewhere (e.g. the Recycle Bin) and then start deleting it. It'll take ages, but at least it'll be out of the way.

I'm pretty sure the time required to delete all those files is an inherent requirement of the task, not an inefficiency in the implementation of deletion.

1

Do you have short file name generation enabled? If so, do you really need it? Removing a file is only a metadata opeartion. So if you've got twice the number of names to remove, the amount of work is significantly higher.

MSalters
  • 8,283
1

Install Cygwin and use rm -r. But that's likely to be overkill.

CarlF
  • 8,872
0

This is provided to compliment and augment the steps taken in the previously mentioned answers.

The given two methods above seem quite effective, but to determine the performance would be difficult unless they are benched against the exact same conditions:

  • Physical Hard Drive Device & Model
    • Preferably, ATTO/ Crystal Disk benchmarked before cloning the data on them
  • Same Partition/ Volume Location & Geometry
    • The way to do this would be defrag the sample partition/ folders and clone them exactly as they are 2 exactly the same types of Hard Drives (from previous step)
  • Then execute the configurations via Batch File and use Powershell Cmdlet to log and measure their performance.

    • Ideally, add some mechanisms to prevent any caching biases due to recency of similar activity.
  • The following are some sample folders (named as total size) that I experimented on but was not able to get any conclusive performance / results.

r.bat

rmdir /s/q 3.2G

rd.bat

del /f/s/q 3.3G > nul
rmdir /s/q 3.3G
  • I'd have posted the output Log files, but I believe the variance is huge due to sizes & distribution of folders files etc.

Powershell:

PS S:\T> Measure-Command { S:\T\rd.bat } > rdlog.txt

PS S:\T> Measure-Command { S:\T\r.bat } > rlog.txt

PS S:\T> Measure-Command { S:\T\rd.bat } > rd_1.7G_log.txt

PS S:\T> Measure-Command { S:\T\r.bat } > r_1.8G_log.txt

PS S:\T> Measure-Command { S:\T\r.bat } > r_2.4G_log.txt

PS S:\T> Measure-Command { S:\T\rd.bat } > rd_2.7G_log.txt

PS S:\T> Measure-Command { S:\T\r.bat } > r_3.2G_log.txt

PS S:\T> Measure-Command { S:\T\rd.bat } > rd_3.3G_log.txt
Alex S
  • 1,019
0

Have you tried either of these two apps?

Be sure to set the number of overwrites to 0 if you want fastest performance. Do this by clicking options then change the value at the bottom of the dialog.

Enter the path to delete in the Source field and then click Delete.

These apps do not put the files in the recycle bin. Use with care.

random
  • 15,201
0

The answer from Hugo is the fastest but it does not remove hidden or system files in the first pass, so if you want a more complete solution use the parameter /a:

del /f /s /q /a foldername > nul
rmdir /s /q foldername

About the BAT file by Magallanes, be very carefull with takeown and icacls, cause hardlinks share the same permissions and owners, so if there are unsaturated hardlinks in the folder you want to remove, using takeown and icacls will change permissions and owners of files outside the folder.

Using the tool ln.exe you can list all the files hardlinked inside the folder and know if they are hardlinked to files outside the folder (unsaturated hardlinks):

ln.exe --enum foldername > HardlinksList.txt
liamZ
  • 151
-1

Deleting folder is faster than deleting multiple files.

So if you prefer to use mouse instead of command prompt, you can create directory, move files there and delete it (with Shift+Del as you said).

danijelk
  • 107