How to Speed Up Folder Deletion Times by 20x or More!
The common way of deleting files and folders in Windows is via Windows Explorer (a.k.a. File Explorer). This method is perfectly acceptable under normal circumstances, but becomes a real drag when dealing with large and complex folder structures. There is, in fact, a significant amount of of overhead when you trigger the standard delete action in Windows including when either emptying the Recycle Bin or directly deleting files via Shift+Del.
Upon flagging a folder for deletion in the traditional fashion, Windows begins by calculating the total folder size, number of items contained within it, and the estimated completion time. This "Preparing to Delete" phase can consume a sizable amount of time itself depending on the contents being deleted. During the actual deletion process, Windows continues to query and report various statistics about the process including how many items are deleted per second, how many remain, the current item being deleted and so forth. You may also be prompted via the Windows dialog if any conflicts arise during the process.
As an example of folder complexity, I have recently been doing a lot of tests with various iterations of the Android NDK across several development machines. The latest Android NDK (r10e) is comprised of around 50,000 Files across 4,100 Folders and totals over 3.15GB in size. Deleting this directory in its extracted format can take up to fifteen minutes when using native Windows Explorer file operations, versus just seconds using the method described below.
A much faster, bare metal approach to deleting large and complex folders in Windows is via the command line. Of course, repeatedly having to navigate directories while executing commands via a terminal quickly becomes a tedious experience. In this post, I will walk through the process of creating a simple batch file and wiring it up to a handy right-click context menu from Windows Explorer to delete sophisticated directories in a hurry and without interruption.
Deleting Files and Folders via Command Prompt
The commands required to perform folder and file delete operations via Command Prompt are quite straightforward.
Files can be deleted using the
DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names
ERASE [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names
There are a handful of optional parameters that can be appended to the
DEL command to control its behavior. To see a description of each, enter
DEL /? from the command prompt. The most important flags when wishing to delete all files across all nested directories include:
- /F - Force deleting of read-only files.
- /Q - Quiet mode, do not ask if ok to delete on global wildcard.
- /S - Deletes specified files from all subdirectories.
To delete all files in a particular directory and its subdirectories, you would first navigate to the directory in question using
cd [PATH] and then execute the following line:
DEL /F/Q/S *.*
If you omit the
/Q flag, you would have to manually verify the deletion of every single file—obviously not what you want when trying to delete a folder's contents as quickly as possible. Likewise, without the
/F flag, any files set to read-only would be ignored and would remain in-tact after the command has completed. The
/S flag is significant as without it you would only be deleting all of the files in the root active directory.
While this method avoids the calculation expense required when deleting through Windows Explorer, the command as shown above will still output the location and deletion status of each file in the iteration. Although this can prove valuable at times, when iterating through thousands of files such output can congest the command's performance. We can instead redirect output to a null location to avoid any on-screen rendering, as such:
DEL /F/Q/S *.* > NUL
Although it may seem like a moot point, the speed gained by merely omitting output is still quite measurable especially when deleting a massive number of items. Upon deleting the ~46,000 files from the NDK package, it took 38 seconds with console output enabled and 29 seconds with output disabled on a standard non-SSD hard drive, scraping off a quarter of the time. By comparison, the same deletion process via a standard Shift+Del in Windows Explorer took an agonizing 11 minutes.
Therefore, using the above command-line operation was over 20 times faster than going through Windows Explorer itself. [Note: When sending a folder to the Recycle Bin via Windows Explorer, it may seem to delete fairly fast on modern editions of Windows, but will still take substantial time when emptying from the Recycle Bin.]
Folders can be deleted using the
RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path
DEL command, There are a couple optional parameters that can be appended to the
RMDIR command to control its behavior. Both of these flags are essential when wishing to delete a folder and all of its sub-folders as fast as possible with no prompts:
- /Q - Quiet mode, do not ask if ok to remove a directory tree
- /S - Removes all directories and files in the specified directory in addition to the directory itself.
RMDIR command with the above flags should be sufficient enough on its own to remove an entire directory tree in most cases. However, Microsoft's documentation warns that this command "cannot delete a directory that contains files, including hidden or system files." I believe that this statement is more applicable to older operating systems such as XP, likely to resolve malicious security flaws from the older
PURGE commands. Regardless, I still prefer to run the
DEL command described previously to ensure each file including read-only has been forcibly deleted before deleting the folders themselves via
RMDIR. There is no measurable speed penalty for using both commands over just one.
To delete a particular directory and all of its subdirectories, you must enter the command:
RMDIR /Q/S [Absolute Path or Relative Folder Name]
Note that this command will not work if you are currently in the directory trying to be deleted. Instead you should navigate to the parent directory via
CD.. and then call the command by referencing the target folder.
Let's Make a Batch File
To enable this functionality without having to continuously enter these commands, we will make a self-contained batch file. I recommend creating a dedicated folder on your hard drive to store small utility programs and batch files, such as 'C:\Programs'. Launch Notepad and paste the following snippet into it.
ECHO Delete Folder: %CD%?
DEL /F/Q/S "%FOLDER%" > NUL
RMDIR /Q/S "%FOLDER%"
When done, select File > Save As and save it as "fastdel.bat" in quotes to your folder of choice (i.e., C:\Programs\). I have added a very basic prompt via the
PAUSE command to allow a single opportunity to abort the deletion process. As soon as you press any key [except CTRL+C to abort or pressing 'X' in corner to close window] the files and folders will be permanently and immediately deleted with the only possible chance of recovery via a third party software recovery application.
This batch file uses the currently active directory as the one to delete. It first stores this working directory in the
FOLDER variable, then navigates to the root drive directory to avoid any operation conflicts. The
RMDIR commands are then called on the original folder and the window will terminate upon completion. Be warned that if you double-click the batch file directly, the folder it resides in will be the target of deletion, so it would delete itself and any siblings or descendants! If you are exceptionally concerned about misfiring this batch file, you could create a more advanced 'Yes/No' confirmation prompt for verification using basic batch file commands.
To enable calling of this fast delete batch file from any computer directory, add the file's directory location to your PATH environmental variable as follows:
- Press WINDOWS KEY + PAUSE/BREAK on your keyboard to open the System Information screen.
- Click on the Advanced System Settings link on the left.
- Under the Advanced tab in the System Variables section, double-click the PATH variable row.
- At the end of the Variable Value field, ensure there is a semicolon (;) and then add the full directory location to the batch file (i.e., C:\Programs\).
- Click OK to close the Edit System Variable prompt, OK again to close the Environmental Variables window, and OK again to close the System Properties.
With this in place, you should be able to open a new command prompt, navigate to any directory you wish to delete, then type
FASTDEL to launch the folder deletion process.
Add Fast Delete Option to the Right-Click Context Menu
All that remains is to add an option to Explorer's Context Menu so that we can right-click any folder and select Fast Delete from the pop-up menu. This adds the convenience of not having to manually launch a Command Prompt window or enter any console commands yourself. You'll have to dig into the Registry to accomplish this, but it is very simple:
- Press WINDOWS KEY + R on your keyboard to open the Run dialog box.
- Enter regedit and press ENTER.
- Navigate to HKEY_CLASSES_ROOT\Directory\shell\
- Right-click on the yellow shell key and select New > Key.
- Enter the name: Fast &Delete then press ENTER.
- Right-click on the yellow Fast &Delete folder just created, then select New > Key.
- Enter the name: command then press ENTER.
- Left-click on the yellow command key just created, then double-click the (Default) entry.
- In the Value Data field, enter: cmd /c "cd %1 && fastdel.bat" then press OK.
Now when you open Windows Explorer / My Computer and navigate to a directory you wish to delete, you can right-click on it and should see the "Fast Delete" option in the menu. The ampersand before the 'D' when creating the key is optional, but will allow you to simply press 'D' after right-clicking a folder to call the fast delete command. WARNING: If you use this keyboard shortcut instead of simply left-clicking the command option, the PAUSE line in the batch file I provided will override and the folder will delete immediately. Again, for safety reasons you may wish to implement a more elegant Yes/No prompt as suggested earlier; as a power user I prefer things to happen quickly with as little user input as necessary.
Note: If you have Cygwin installed and configured, you can also remove directories and files via
rm -rf [path]. However, my own tests did not reveal any measurable speed improvements over the integrated method described in this article.
November 16, 2015 Update: I recently observed that the original batch file script I provided above did not compensate for paths with spaces in them. For example, if you had two folders 'Test' and 'Test Two' and right-clicked 'Test Two' to delete via the script, it would actually find the 'Test' folder and delete its contents instead. This has been amended by surrounding the paths in quotes.