Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active January 12, 2016 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leodutra/e56754f5129e71f5ab72 to your computer and use it in GitHub Desktop.
Save leodutra/e56754f5129e71f5ab72 to your computer and use it in GitHub Desktop.
Remove folders and files in Windows
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 at Super User.
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.
@echo off
del /f/s/q %1 > nul
rmdir /s/q %1
@elifarley
Copy link

Windows tools sometimes don't work as they should. See this for instance:

Bug 727551 - Create a Windows-native alternate to msys rm.exe to avoid common problems deleting files
https://bugzilla.mozilla.org/show_bug.cgi?id=727551

And here's an alternate tool to delete files in Windows:
https://github.com/vvuk/winrm

@leodutra
Copy link
Author

Alternate runnable for Node.js
https://github.com/isaacs/rimraf

@leodutra
Copy link
Author

@elifarley thanks for the tip. I had this problem with some long paths on Windows 8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment