"rm -rf" equivalent for Windows?
Solution 1
RMDIR or RD if you are using the classic Command Prompt (cmd.exe):
rd /s /q "path"
RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path
/S Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree.
/Q Quiet mode, do not ask if ok to remove a directory tree with /S
If you are using PowerShell you can use Remove-Item
(which is aliased to del
, erase
, rd
, ri
, rm
and rmdir
) and takes a -Recurse
argument that can be shorted to -r
rd -r "path"
Solution 2
admin:
takeown /r /f folder
cacls folder /c /G "ADMINNAME":F /T
rmdir /s folder
Works for anything including sys files
EDIT: I actually found the best way which also solves file path too long problem as well:
mkdir \empty
robocopy /mir \empty folder
Solution 3
RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path
/S
Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree./Q
Quiet mode, do not ask if ok to remove a directory tree with/S
Solution 4
Go to the path and trigger this command.
rd /s /q "FOLDER_NAME"
/s : Removes the specified directory and all subdirectories including any files. Use /s to remove a tree.
/q : Runs rmdir in quiet mode. Deletes directories without confirmation.
/? : Displays help at the command prompt.
Solution 5
You can install cygwin, which has rm
as well as ls
etc.
Peter Ramos
Updated on July 08, 2022Comments
-
Peter Ramos almost 2 years
I need a way to recursively delete a folder and its children.
Is there a prebuilt tool for this, or do I need to write one?
DEL /S
doesn't delete directories.DELTREE
was removed from Windows 2000+ -
Wedge almost 16 yearsIt's worth pointing out that for large numbers of files, rmdir /s /q is typically significantly faster than the equivalent "select dir, shift + delete" operation in explorer.
-
Tom Mayfield about 14 yearsHow about using flags to match directory names? If I want to drop all directories under foo\,
rmdir /s /q foo\*
gives an error for syntax incorrect. -
Dirk Groeneveld about 11 yearsThis doesn't delete files, like rm -rf does, and it also returns a non-zero value when the directory doesn't exist, so
rd /s /q foo && echo "yay"
will fail if directory "foo" doesn't exist. -
alexey over 10 years@Derek, Doskey could help you create aliases for ls=dir: devblog.point2.com/2010/05/14/…
-
Eugene over 10 yearsWhat if dir. not empty and some files get
Access is denied
and othersThe process cannot access the file because it is being used by another process
? -
bbqchickenrobot over 10 yearsworked for me in combination w/ rmdir /s /q as some files were locked and rmdir would fail on those. del / foldername nuked the locked files which then allowed rmdir to get rid of root dir. Nice.
-
ford prefect almost 10 yearsI just tried this in seven and you need to do /S and /Q (caps)
-
Léon Pelletier over 8 yearsMy hero! And one tip: You create your empty folder at C:\empty, then once inside each crazy folder, one can just do
robocopy /mir c:\empty .
-
Alex Hall over 8 yearsMystified here. I know that I tried this command and it did not work if there were files in the directory tree; now I go back and . . . magically it seems to work. Does anyone else experience inconsistent behavior with this?!
-
Brain almost 8 yearsYes, I experience inconsistent behavior all the time. It says it cannot delete the directory because it is blocked by some process. But there are no processes blocking the directory.
-
Jarda almost 8 yearsThe
takeown
helped me as I copied folder from Linux system with rsync by mistake and I had no privileges to remove that folder... Not even the robocopy worked. Thanks -
Thomas over 7 yearsThanks for the parameter explanation. Accepted answer does not do this, unfortunately.
-
Clay about 7 yearsUse
if exist myfolder ( rmdir /s/q myfolder )
if you don't know whether the folder will exist or not -
Jon Gunter almost 7 yearsThis doesn't seem to work in PowerShell? I'm getting "A positional parameter cannot be found that accepts argument '/q'."
-
Duncan Smart almost 7 years@JonGunter for Powershell you use the Remove-Item cmdlet msdn.microsoft.com/en-us/powershell/reference/5.1/…
-
Gangnus over 6 yearsVery nice answer. It works. (all other answers on the page don't). But cacls is deprecated. Is it possible to make a contemporary answer, please? The second variant already works and is not deprecated.
-
Gangnus over 6 yearsMaybe it worked in some older windows. But now it does not work at all.
-
Matt Hancock almost 6 years
help rmdir
in windows 7 indicates/S
should be used to remove the whole directory tree and/Q
should be used for quiet mode. -
Artif3x over 5 yearsSimplest, perfect answer to the question. Should be the accepted one.
-
Denialos over 5 yearsA solid solution to the problem as it's OS-agnostic.
-
Ralf about 5 yearsThis doesn't do anything for me. Could someone please explain how it is supposed to work? (robocopy) - It might delete contents of folders, but not folders themselves
-
George Pligoropoulos over 3 yearsActually this is the answer. Thanks @Clay
-
Bilal over 3 years@wbkang when I run
cacls "C:\B" /c /G "ADMINNAME":F /T
I getNo mapping between account names and security IDs was done.
-
wbkang over 3 years@bilal substitute"adminname" with your actual account name.
-
Yerrapotu ManojKiran over 3 years@wbkang For me rmdir was not working because few files were having long paths, actually your solution of copying empty folder to folder is a smart solution, thank you
-
Nostalg.io about 3 yearsTHANK YOU! This suggestion actually worked in Windows Terminal.
-
Ruslan López about 3 years
The system cannot find the path specified
:C -
Patrick Artner almost 3 years
rm
is just (one of the plentydel, erase, rd, ri, rm, rmdir
) aliases forremove-item
wich is already mentioned in answers stackoverflow.com/a/63745519/7505395 and stackoverflow.com/a/53859156/7505395 and the accepted answer stackoverflow.com/a/97896/7505395 (which does not explicitly state to use force though - but all the others do) -
David Jones almost 3 yearsIt's also worth pointing out the saying
rd -r
out loud makes you sound like a pirate. -
faester almost 3 yearsWell -Force makes a difference. I found the other answers confusing.
-
Ray Hulha almost 3 yearsThis is a great start, but the batch file is not needed: cmd.exe /s /c rmdir "%V" is enough