"rm -rf" equivalent for Windows?

801,805

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.

Share:
801,805
Peter Ramos
Author by

Peter Ramos

Updated on July 08, 2022

Comments

  • Peter Ramos
    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
    Wedge almost 16 years
    It'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
    Tom Mayfield about 14 years
    How 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
    Dirk Groeneveld about 11 years
    This 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
    alexey over 10 years
    @Derek, Doskey could help you create aliases for ls=dir: devblog.point2.com/2010/05/14/…
  • Eugene
    Eugene over 10 years
    What if dir. not empty and some files get Access is denied and others The process cannot access the file because it is being used by another process?
  • bbqchickenrobot
    bbqchickenrobot over 10 years
    worked 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
    ford prefect almost 10 years
    I just tried this in seven and you need to do /S and /Q (caps)
  • Léon Pelletier
    Léon Pelletier over 8 years
    My 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
    Alex Hall over 8 years
    Mystified 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
    Brain almost 8 years
    Yes, 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
    Jarda almost 8 years
    The 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
    Thomas over 7 years
    Thanks for the parameter explanation. Accepted answer does not do this, unfortunately.
  • Clay
    Clay about 7 years
    Use if exist myfolder ( rmdir /s/q myfolder ) if you don't know whether the folder will exist or not
  • Jon Gunter
    Jon Gunter almost 7 years
    This doesn't seem to work in PowerShell? I'm getting "A positional parameter cannot be found that accepts argument '/q'."
  • Duncan Smart
    Duncan Smart almost 7 years
    @JonGunter for Powershell you use the Remove-Item cmdlet msdn.microsoft.com/en-us/powershell/reference/5.1/…
  • Gangnus
    Gangnus over 6 years
    Very 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
    Gangnus over 6 years
    Maybe it worked in some older windows. But now it does not work at all.
  • Matt Hancock
    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
    Artif3x over 5 years
    Simplest, perfect answer to the question. Should be the accepted one.
  • Denialos
    Denialos over 5 years
    A solid solution to the problem as it's OS-agnostic.
  • Ralf
    Ralf about 5 years
    This 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
    George Pligoropoulos over 3 years
    Actually this is the answer. Thanks @Clay
  • Bilal
    Bilal over 3 years
    @wbkang when I run cacls "C:\B" /c /G "ADMINNAME":F /T I get No mapping between account names and security IDs was done.
  • wbkang
    wbkang over 3 years
    @bilal substitute"adminname" with your actual account name.
  • Yerrapotu ManojKiran
    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
    Nostalg.io about 3 years
    THANK YOU! This suggestion actually worked in Windows Terminal.
  • Ruslan López
    Ruslan López about 3 years
    The system cannot find the path specified :C
  • Patrick Artner
    Patrick Artner almost 3 years
    rm is just (one of the plenty del, erase, rd, ri, rm, rmdir) aliases for remove-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
    David Jones almost 3 years
    It's also worth pointing out the saying rd -r out loud makes you sound like a pirate.
  • faester
    faester almost 3 years
    Well -Force makes a difference. I found the other answers confusing.
  • Ray Hulha
    Ray Hulha almost 3 years
    This is a great start, but the batch file is not needed: cmd.exe /s /c rmdir "%V" is enough