Fastest way to delete a directory tree in Windows

Our nightly build was taking > 30 minutes to delete our source tree before getting latest (Windows is not quick to delete a large number of small files), which was comparable to the time taken to actually compile the source. There are stackoverflow and superuser articles that discuss the fastest way to do deletes, however there’s no magic solution. If you find you are regularly needing to delete large amounts of files (e.g. as part of the nightly build process), your best option is to create a new partition on your HDD (or install a new HDD) and store all the files you will be deleting on there. Then you can erase them all in about 2 seconds by doing a quick format of the drive! The only trick is how to programatically format a drive, since there is no (documented) API for this in Windows. There’s Win32_Volume.Format (WMI) but it’s only available in server OSs, and then there’s SHFormatDrive, which shows a dialog.

However, provided your partition (in this case Z:) has no label, you can do this:

echo Y | format Z: /FS:NTFS /X /Q
An inferior alternative to this is to use a VBScript that uses SendKeys like this:

set WshShell = CreateObject("WScript.Shell")
wshShell.run "c:windowssystem32format.com Z: /FS:NTFS /V:QuickWipeDrive /X /Q"
wscript.sleep 1000
wshshell.sendkeys "QuickWipeDrive" & "{ENTER}"
wscript.sleep 1000
wshshell.sendkeys "Y" & "{ENTER}"
wscript.sleep 5000

The downside of this script is that it will only work when the computer is not locked since SendKeys requires a console session that is logged in an active.

2 thoughts on “Fastest way to delete a directory tree in Windows

  1. Nime says:

    If you cannot change a path in an application’s settings ie; “c:\my app\temp” create a hardlink between “c:\my app\temp” and drive Z:

  2. Nime says:

    If you cannot change a path in an application’s settings ie; “c:my apptemp” create a hardlink between “c:my apptemp” and drive Z:

Comments are closed.