47

I have a Windows XP box with an NTFS disk and deleting large amounts of files is extremely slow. If I select a folder that contains a large number of files in a tree of folders and delete (using shift-del to save the recycle bin) it takes time that seems to be directly proportional to the number of files within the folder before it even pops up the confirmation box. It then takes an even longer time to delete each file in the folder.

Is there a way to delete a folder in Windows and not having the time taken proportional to the number of files within it?

user 99572 is fine
  • 3,397
  • 3
  • 30
  • 43
Sindri Traustason
  • 878
  • 3
  • 11
  • 17
  • 2
    And that is a duplicate of http://superuser.com/questions/19762/mass-deleting-files-in-windows – Hugo May 27 '11 at 11:55

8 Answers8

79

Is there a way to delete a folder in Windows and not having the time taken proportional to the number of files within it?

I don't think so, but some methods are clearly much quicker than others.

The worst way is to send to Recycle Bin: you still need to delete them. Next worst is shift+delete with Windows Explorer: it wastes loads of time checking the contents before starting deleting anything.

Next best is to use rmdir /s/q foldername from the command line. del /f/s/q foldername is good too, but it leaves behind the directory structure.

The best I've found is a two line batch file with 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

This is nearly three times faster than a single rmdir, based on time tests with a Windows XP encrypted disk, deleting ~30GB/1,000,000 files/15,000 folders: rmdir takes ~2.5 hours, del+rmdir takes ~53 minutes. More info here.

This is a regular task for me, so I usually move the stuff I need to delete to C:\stufftodelete and have those del+rmdir commands in a deletestuff.bat batch file. This is scheduled to run at night, but sometimes I need to run it during the day so the quicker the better.

Hugo
  • 2,940
  • 1
  • 21
  • 22
  • 2
    Upvote for including `> nul` – Navigatron Apr 06 '17 at 22:42
  • 1
    Windows was going to make me wait to scan many thousands of files from a backup of an old SDK. It was going to take at least an hour, this took may be 10 minutes in my case. I've put it into a bat file for repeat use: https://gist.github.com/DavidEdwards/61d4d336232284b33b237b04da5bfe10 – Knossos Dec 01 '17 at 11:26
  • See also same answer with more info at https://superuser.com/questions/19762/mass-deleting-files-in-windows – Elliott Beach Feb 10 '19 at 19:20
3

Is there a way to delete a folder in Windows and not having the time taken proportional to the number of files within it?

Well, yes, format the partition. I'm a bit surprised nobody suggested that in the previous 9 years.

It's pretty radical, but if you anticipate doing this frequently for a specific folder, it might be worthwhile creating a separate partition for it.


If that's too radical, the other answers are your only hope. There is a good explanation why on serverfault. It's for linux and XFS filesystems, but the same logic applies here. You can't improve much on build-in OS functions.

However, if you know the paths to all the files you wish to delete, then you can save on calls that list the directory contents and call remove directly, saving some overhead. Still proportional to the number of files though.

Personally, I like some from of progress report to ensure myself that the program didn't die. So I like to delete stuff via python. For example, if all the files are in one directory without sub-directories:

import tqdm
import sys
import os

location = sys.argv[1]
directory = os.fsencode(location)

with os.scandir(directory) as it:
    for dir_entry in tqdm.tqdm(it):
        try:
            os.remove(dir_entry.path)
        except OSError:
            pass  # was not a file

This deletes about 250 files/s on my 12 year old SEAGATE ST3250620NS. I'd assume it will be much faster on your drive.

However, at this point it's just micro-optimization, so won't do very much unless you have millions of files in one directory. (like me, lol, what have I done D:)

2

I used Hugo's original answer to create a .bat file that I use when deleting NPM projects. I added a path variable and only have to copy and paste the path once. Double-click the .bat file and it does all the work - no need to type everything.

set path_to_delete="FOLDER_PATH"
del /f/s/q %path_to_delete% > nul
rmdir /s/q %path_to_delete%

Example usage:

set path_to_delete="C:\Projects\My React Project"
del /f/s/q %path_to_delete% > nul
rmdir /s/q %path_to_delete%
Halcyon
  • 567
  • 2
  • 6
  • 10
  • It's dangerous if the user confuses your "path variable" with [the PATH variable](https://en.wikipedia.org/wiki/PATH_(variable)). I'll try to rename the variable in your answer. – root Dec 06 '21 at 12:38
1

On the newer versions of Windows 10 that come with the Linux subsystem (basically an Ubuntu shell) you can use Linux commands like rm -rf directory. This seems to be the fastest way on my machines.

Foo Bar
  • 1,440
  • 2
  • 13
  • 25
1

Install gnutools for windows and run:

find YOURFOLDER -type d -maxdepth 3 | xargs rm -Rf
Jawa
  • 3,619
  • 13
  • 31
  • 36
useless
  • 111
  • 2
0

Make sure you're not backing up files to the cloud and trying to delete them at the same time!

With many cloud backup solutions files will get locked while they are being backed up and then you have to wait for them to be backed up.

If you're having this issue with say a temp directory (or something that doesn't need backing up) make sure that temp directory isn't selected in your backup set.

Simon
  • 662
  • 7
  • 21
0

I've found that folders with several layers of directories tend to really slow down Window's ability to remove them quickly. I was working on a project where it took 5 levels to get to the node_modules folder, which is always a beast to delete, even with

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

What I end up doing in this situation is navigating down to the node_modules folder or whatever directory has the deepest levels and just start selecting and deleting about a dozen or so directories at a time. If I get multiple deletes going, this forces the Recycle Bin to work in parallel processes rather than the single thread I believe it uses, dramatically speeds up the process.

Once my deepest directory is empty, I go up a few levels and do the same thing. This has cut down deletes that have taken me over an hour to just a few minutes.

Its a very manual process and could likely be scripted with some success, but its what's worked for me

0

did you try using command prompt

rmdir /s /q foldername
Sid110307
  • 137
  • 6
nEJC
  • 148
  • 4
  • 7
    This is nonsense. Operation may be slow because GUI needs to count all files to estimate required time but not due to repainting. – Bender Nov 30 '09 at 13:22
  • wrong ... I work with large folders constantly (mostly win2k server), and use TotalCommander to move/copy/delete stuff. I've noticed that if I minimize TC or put another application window on top of TC stuff gets done at least 50% faster. TC is still repainting visal stuff, but everything is ignored in compositing... – nEJC Nov 30 '09 at 13:36
  • 9
    Is your computer so slow that GUI operations impact disk I/O performance? Or is TotalCommander just incredibly poorly coded? The disk is hundreds or thousands of times slower than the CPU, RAM and Video card. If graphics are slowing down your disk writes you have major issues. – Mr. Shiny and New 安宇 Nov 30 '09 at 14:04
  • 1
    The question had "not having the time taken proportional to the number of files" for a reason. I'm not looking for 50% faster. – Sindri Traustason Dec 01 '09 at 10:44
  • @Sindri If you do it the command prompt way there should be almost no delay before system starts working its magic. As far as I understand this preprocessing is where your problem is. – nEJC Dec 02 '09 at 15:35
  • @Mr. Shiny computers I'm talking about are not slow, but are servers and don't have the latest DX11 gfx card installed, so yes GUI is slower than it could be on better video HW my thinking is that TC works something like this: delete file -> repaint progress bar -> get next file -> loop. If TC is hidden from viewport repainting doesn't happen, but TC doesn't know that, windows compositing does, so its reasonable to assume that windows GUI is slow at repainting, not TC (but yes, most probably both are at fault) – nEJC Dec 02 '09 at 15:49