Fastest Way to Delete Large Folders in Windows

How to Speed Up Folder Deletion Times by 20x or More!

Windows Fast Delete FileThe 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.

Fast Delete Folder

Deleting a large and complex folder via Windows Explorer can take a long time.

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 (a.k.a. ERASE) command.

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:

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:

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 (a.k.a. RD) command.

Like the 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.

Technically, the 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 DELTREE and 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:

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.

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 DEL and 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:

  1. Press WINDOWS KEY + PAUSE/BREAK on your keyboard to open the System Information screen.
  2. Click on the Advanced System Settings link on the left.
  3. Under the Advanced tab in the System Variables section, double-click the PATH variable row.
  4. 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\).
  5. 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:

  1. Press WINDOWS KEY + R on your keyboard to open the Run dialog box.
  2. Enter regedit and press ENTER.
  3. Navigate to HKEY_CLASSES_ROOT\Directory\shell\
  4. Right-click on the yellow shell key and select New > Key.
  5. Enter the name: Fast &Delete then press ENTER.
  6. Right-click on the yellow Fast &Delete folder just created, then select New > Key.
  7. Enter the name: command then press ENTER.
  8. Left-click on the yellow command key just created, then double-click the (Default) entry.
  9. In the Value Data field, enter: cmd /c "cd %1 && fastdel.bat" then press OK.

Fast Delete

 

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.

36 thoughts on “Fastest Way to Delete Large Folders in Windows

  1. Thanks, the tips is very detailed and step by step πŸ˜€ I read about the slowness of windows explorer deleting speed many time but usually they just end up talking about using cmd without any smart/quick intergrated trick like this. If everytime you've to open up a cmd and type things (which are simple enough but tedious for long process) to delete one or many large folders then you leave out a big amount of time saved by the method itself due to human slow interaction... Automatic and intergrated things is nice way to do this kind of delete task. Good share πŸ˜€

  2. Thanks a lot for this script. I've been struggling to delete a huge folder, and your script makes things way more effective.

  3. When I create the fastdel.bat file and run, the rmdir does not remove the top level directory, only the ones underneath it. Just understand that this situation exists. With the information above I created this batch file, put into a batch folder. It's not perfect either but the output is a lot less.
    Go to the directory with files and folders you wish to delete, and execute the batch file.
    for example...
    c:
    cd\folder1
    c:\batch\fastrmdir.bat

    All the folders underneath folder1 will be deleted except HIDDEN ones.

    REM fastrmdir.bat
    setlocal ENABLEDELAYEDEXPANSION

    set wrkfile=" "

    Echo "Ready to delete?"
    Pause

    for /D %%n in ("*.*") do (
    set wrkfile="%%n"
    echo Now Deleting...!wrkfile!
    rmdir /q/s !wrkfile!
    )

  4. This seems like a good approach. Unfortunately, I have 9 servers with 2+ million files and what I want to do, is delete all the files that are older than 30 days. Can you suggest and option for this approach?

    • Hi Jeffrey,

      It looks like Chris made one possible recommendation for you. I would alternatively suggest taking a look at the free synchronization tool DirSyncPro, which allows for very granular and advanced syncing of files and directories across multiple drives or devices. Using a Custom sync job, you can create filters based specifically on when the files were last modified, such as 30 days ago. You can then schedule the jobs to run at set intervals automatically. Although this program is mostly made for backup purposes, with the advanced options you should be able to create a job that excludes files greater than 30 days older, and then also set the 'Deletion' options to 'Force deleting excluded files/directories from Dir A/B' if that makes sense. Good luck!

  5. @Jeffrey, it'll probably take a long time but you can possibly do something like this:

    forfiles /s /m *.* /d -30 /c "cmd /c echo del @path"

    I have included an extra 'echo' in the middle - just so you don't accidentally delete things by testing it.

    For the live version, remove the word echo so it has "cmd /c del @path".

    Give it a test first, as I haven't fully tested this.

  6. The fastest way that I've found to delete files, and has worked this way even back to dos 3.3, is running 'del *.*' inside the directory you want to delete. I don't the know the exact mechanism behind this (a search on why brought me here. I've got 10 of these delete sessions going right now), but I suspect it is the way the file entries are removed from the file allocation table.

  7. At some code samples in this article, the greater sign ">" is replaced with its HTML tag >
    This may be misleading for readers not aware of this.

    • Thanks for noticing, Frank. I've corrected the issue and will stop this blog from auto-correcting code in the future as it's been a problem for some time.

  8. Hmmm - seems that this blog software takes all user input as is without filtering HTML code. This may be abused by malicious users to add scripts etc - not a good thing. OK, lets try this:

  9. Thanks Matt. Very detailed and well organized instructions. I started watching your stuff a long time ago and just rediscovered it. Also from Wisconsin originally but from the big city - Milwaukee. Lol. Hope you are wildly successful but always find time to provide useful tips like this.

  10. Fot those who are lazy (or have problems), I made an "Installer". It is just an EXE file. You must run it as adminastrator, because it must write in HKEY_CLASSES_ROOT. It only creates a folder (FastAndDelete) with the fastdel.bat in it on you main hard drive. after that it creates everything necessary in the registry and makes the entry in the PATH variable. Read the Info.txt for details.

    http://s000.tinyupload.com/?file_id=77146951311623759834

    Have fun!

  11. Thank you for this. The only problem I had was getting the right click shortcut to delete the folders working. This is what I had to put into the final regedit key to make everything happen.

    cmd /c cd "%1" && fastdel.bat

  12. Also you can condense the regedit key a little further since you already have the path working this should be fine.

    cmd /c cd "%1" && fastdel

    notice there is no .bat extension. It wasnt needed for my windows 7 machine.

  13. Works well local. But doesn't work with UNC path (such as IP network drive, e.g \\192.168.1.100\data\temp\) Way to tweak this *.bat file or *.reg to be able work with UNC paths? Thanks for reading.

    • Hi Yuu,

      I modified the script slightly to work with UNC path to network drive. I had to change the syntax so that we delete the first parameter instead of assuming we want to delete current directory (e.g. fastdel "my dir"):

      @ECHO OFF
      ECHO Delete Folder: %1? ^ to quit
      PAUSE
      rem Remove absolute path
      SET FOLDER=%~n1
      rem Use pushd to cd to parent directory
      rem (workaround for UNC path to network drives)
      SET PARENT=%1\..
      pushd "%PARENT%"
      DEL /F/Q/S "%FOLDER%"
      RMDIR /Q/S "%FOLDER%"
      popd
      PAUSE
      EXIT

      Note you have to change the registry command due to the new syntax:
      cmd /c "fastdel.bat "%1""

      Thanks.

  14. Here is how I do it basically:

    set empty_dir=%TEMP%\%RANDOM%
    mkdir "%empty_dir%"
    robocopy "%empty_dir%" "dir_to_delete" /mir /w:1 /r:10

    I have this wrapped up in a batch file that asks for confirmation first, etc. but at the core it simply uses robocopy.exe, which has being included in Windows since a while now.

  15. Pingback: 1 – Fastest Way to Delete Large Folders in Windows – Matt's Repository

  16. Pingback: New top story on Hacker News: Fastest Way to Delete Large Folders in Windows – Matt’s Repository | The Internet Junkyard

  17. Pingback: Fastest Way to Delete Large Folders in Windows (2015) | ExtendTree

  18. Works but remember you will have a harder time undeleting any files. You'll notice command prompt delete doesn't "move" it to the recycle bin. So, be careful when deleting through command prompt.

    As for deleting files older than 30 days... forfiles is fine, but I prefer delage32 which is a free tool with straight forward option parameters.

  19. FAR Manager helps,
    being a command shell around (DEL, COPY, FIND), it deletes files faster then standard windows explorer...

    and also copies, moves files faster too.

  20. I modified the batch script slightly to work with UNC path to a network drive. Note that it now deletes the first parameter to the script instead of assuming we want to delete current directory:

    @ECHO OFF
    ECHO Delete Folder: %1? ^ to quit
    PAUSE
    rem Remove absolute path
    SET FOLDER=%~n1
    rem Use pushd to cd to parent directory
    rem (workaround for UNC path to network drives)
    SET PARENT=%1\..
    pushd "%PARENT%"
    DEL /F/Q/S "%FOLDER%"
    RMDIR /Q/S "%FOLDER%"
    popd
    PAUSE
    EXIT

    You also have to modify the registry command slightly since I changed the command syntax:
    cmd /c "fastdel.bat "%1""

    Thanks!

    • Here's a version that works if you have a "." in the folder name:

      @ECHO OFF
      ECHO Delete Folder: %1? ^ to quit
      PAUSE
      rem Remove absolute path
      SET FOLDER=%~n1%~x1
      rem Use pushd to cd to parent directory
      rem (workaround for UNC path to network drives)
      SET PARENT=%1\..
      pushd β€œ%PARENT%”
      DEL /F/Q/S β€œ%FOLDER%”
      RMDIR /Q/S β€œ%FOLDER%”
      popd
      PAUSE
      EXIT

  21. I liked your right-click option so much, that I wrote a fast copy script too:

    @ECHO OFF
    ECHO Copy Folder: %1? ^ to quit
    PAUSE
    SET S_INPUT=%1
    SET /p D_INPUT=Enter destination folder:
    rem Remove quotes
    SET SRC=%S_INPUT:"=%
    SET DEST=%D_INPUT:"=%
    robocopy /mir "%SRC%" "%DEST%"
    PAUSE
    EXIT

    On the registry I added a "Fast &Copy" key with the following command:
    cmd /c "fastcopy.bat "%1""

  22. TeraCopy ( its free ) also has an easy to use DRAG AND DROP option to fast delete a folder and all sub folders and contents. Start TeraCopy use file explorer to drag folder onto teracopy and choose fast delete. its very fast. I know its not as versatile as a batch file but it has a place for ease of use.

  23. From "Let's make a batch file," I created the batch file in W/10 64 bit but Windows would not let me place it in Program files, so i put it in the default documents folder, and did the rest as instructed (and restarted explorer.exe). But when i choose a folder to delete and hit Fast Delete in the R. click ,menu, then the command prompt flashes into view for a second but nothing happens.

    So i installed unlocker http://www.majorgeeks.com/files/details/iobit_unlocker.html and which provides a Delete option and which worked quickly. Thank God for those who make such helps. I send some feedback to MS on this also:

    If you choose a folder to be deleted, such as a 2GB folder in the C drive on a SSD hard drive, then it takes over 7 minutes just to calculate it. Even with a 4.2 Ghz 4-core CPU. You need a fast delete right click option.

  24. Like a previous poster, I found that when I tried the instructions as written above, it would try to delete the C:\Windows\System32 folder, no matter which folder I chose in Explorer. What's happening is that it's only working for folders on drive C:, and if you try to delete a folder on any other drive - it instead deletes the C:\Windows\System32 folder.

    To fix that, use this registry value instead:

    cmd /c "cd /d %1 && fastdel.bat"

    You must use the "/d" parameter or the CD command only works on the current drive, which from the Explorer context menu is C:. To make matters worse, the default directory from inside the context menu happens to be Windows\System32.

Leave a Reply