89

rm is to remove item, but what is the parameter -rf do or signify?


Whenever I typed help -rf it printed the entire list of available commands in powershell. What happens if you type rm -rf in powershell? From reading around I've gathered that it will delete everything on the drive? I'm not sure?

Also, is rm -rf same as rm -rf /?

3 Answers 3

161

PowerShell isn't UNIX. rm -rf is UNIX shell code, not PowerShell scripting.

  • This is the documentation for rm (short for Remove-Item) on PowerShell.
  • This is the documentation for rm on UNIX.

See the difference?

On UNIX, rm -rf alone is invalid. You told it what to do via rm for remove with the attributes r for recursive and f for force, but you didn't tell it what that action should be done on. rm -rf /path/to/delete/ means rm (remove) with attributes r (recursive) and f (force) on the directory /path/to/remove/ and its sub-directories.

The correct, equivalent command on PowerShell would be:

rm C:\path\to\delete -r -fo

Note that -f in PowerShell is ambiguous for -Filter and -Force and thus -fo needs to be used.

6
  • 1
    For completeness, the PowerShell equivalent would be rm \ -r -f. You can abbreviate parameter names in PowerShell, but you can't stack them. May 7, 2012 at 18:15
  • 6
    This answer is incorrect. If you do rm <path> -r -f in PowerShell, but the path does not exist, an exception is thrown. In unix, if you run rm -rf <path> on a path which does not exist, the command exits successfully. The two commands are not equivalent. Feb 1, 2016 at 22:09
  • 4
    The PowerShell equivalent is (no longer?) true. If you do that you will receive an error Remove-Item : Parameter cannot be processed because the parameter name 'f' is ambiguous. Possible matches include: -Filter -Force. You can use rm C:\path\to\delete -r -fo however.
    – Tarulia
    Nov 7, 2017 at 18:19
  • 1
    The PowerShell command does not work. When I try rm path\to\delete -r -fo I get error message: Remove-Item: Cannot remove item path\to\delete: The directory is not empty. : 'path\to\delete' I am using PS version 7.1.1
    – j.karlsson
    Jan 31, 2021 at 19:14
  • 1
    I found out why (where) the PS command does not work. If I try it in a folder handled by OneDrive it fails, but it works otherwise. Microsoft please fixit!
    – j.karlsson
    Jan 31, 2021 at 19:24
49

You have to use:

Remove-Item C:\tmp -Recurse -Force

or (short)

rm C:\tmp -Recurse -Force
2
  • So.. waht does rm -rf / does in powershell?
    – BK_
    May 4, 2012 at 7:00
  • Gotcha, you didn't specify at first, so I wasn't sure.
    – BK_
    May 4, 2012 at 7:07
6

This is the one-liner that behaves like rm -rf. It first checks for the existence of the path and then tries removing it.

if (Test-Path ./your_path) { rm -r -force ./your_path}
1
  • 2
    Thank you that's the right answer I was looking for. Many people forget that UNIX's -f actually allows to silently ignore a nonexistent directory. Nov 11, 2021 at 2:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.