TL;DR – Remove-Item -Recurse -Force <path>
On linuxy systems, rm -rf <path>
means “remove this path and everything under it, dammit.” If the files aren’t writeable, but you own the files, it deletes them anyway.
In PowerShell, rm
is aliased to Remove-Item
, but it doesn’t accept -rf
.
Windows Terminal> rm -rf foo
Remove-Item : A parameter cannot be found that matches parameter name 'rf'.
I can call Remove-Item
without -rf
, and it still works, but it asks me for confirmation.
Windows Terminal> rm foo
> rm foo Confirm The item at C:\Users\jessi\code\jessitron\injectify\foo has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
I can say “A” for “Yes to all” and then it does what I want.
Unless! There are files in there with do-not-write permissions. There are zillions of these in the .git
directory, for instance. Sometimes I want to wipe that out and start a fresh git history. Then I get a zillion errors:
Remove-Item : Cannot remove item C:\Users\jessi\code\jessitron\injectify\.git\objects\bf\203afb5389983253213646fa165f749fdcaf09: You do not have sufficient access rights to perform this operation. At line:1 char:1 + Remove-Item -Recurse .git + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (203afb5389983253213646fa165f749fdcaf09:FileInfo) [Remove-Item],
Here’s the secret formula:
Remove-Item -Recurse -Force <path>
Recurse is for deleting all the directories and files underneath; Force is for overcoming those permissions errors. This will get rid of .git
for me.
It’s a little sad that -rf
doesn’t translate to -Recurse -Force
. It is not an unreasonable abbreviation. Maybe in a future PowerShell release, it will.