78

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 /?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
BK_
  • 783
  • 1
  • 5
  • 5

3 Answers3

148

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.

Tarulia
  • 795
  • 5
  • 10
Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • 1
    For completeness, the PowerShell equivalent would be `rm \ -r -f`. You can abbreviate parameter names in PowerShell, but you can't stack them. – JasonMArcher May 07 '12 at 18:15
  • 6
    This answer is incorrect. If you do `rm -r -f` in PowerShell, but the path does not exist, an exception is thrown. In unix, if you run `rm -rf ` on a path which does not exist, the command exits successfully. The two commands are not equivalent. – djhaskin987 Feb 01 '16 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 07 '17 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 '21 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 '21 at 19:24
  • and if (when) you get permissions errors, see this post: https://stackoverflow.com/questions/43767793/you-require-permission-from-myself-to-delete-folder-how-to-fix (shut down applications that may have a lock on the files/folders you're trying to delete). – Brian D Sep 16 '21 at 18:23
47

You have to use:

Remove-Item C:\tmp -Recurse -Force

or (short)

rm C:\tmp -Recurse -Force
Kai Sternad
  • 22,214
  • 7
  • 47
  • 42
4

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}
Amin Ya
  • 1,515
  • 1
  • 19
  • 30
  • 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. – Johan Boulé Nov 11 '21 at 02:42