Remove-Item -Recurse -Force can't remove symlinks
Asked Answered
Q

3

7

It seems that Remove-Item -Recurse -Force cannot deal with removing symlinks. How do I recursively delete everything from a given directory with symlinks made all over the place?

MWE:

PS C:\Users\Administrator\Desktop\test> mkdir foo
    Directory: C:\Users\Administrator\Desktop\test
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        7/30/2020  11:58 AM                foo

PS C:\Users\Administrator\Desktop\test> mkdir bar
    Directory: C:\Users\Administrator\Desktop\test
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        7/30/2020  11:58 AM                bar

PS C:\Users\Administrator\Desktop\test> New-Item -ItemType SymbolicLink -Path foo -Name bar -Value C:\Users\Administrator\Desktop\test\bar
    Directory: C:\Users\Administrator\Desktop\test\foo
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d----l        7/30/2020  11:59 AM                bar

PS C:\Users\Administrator\Desktop\test> Remove-Item -Recurse -Force foo
Remove-Item : There is a mismatch between the tag specified in the request and the tag present in the reparse point
At line:1 char:1
+ Remove-Item -Recurse -Force foo
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Remove-Item], Win32Exception
    + FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.RemoveItemCommand
Quoth answered 30/7, 2020 at 10:4 Comment(0)
Q
7

The actual CMD equivalent to Remove-Item -Recurse -Force is:

cmd /c rmdir /s /q foo

as it will delete symlinks also in subdirectories. This seems to be good enough for me.

Quoth answered 30/7, 2020 at 13:32 Comment(0)
I
3

Looks like you are running into the same issue as mentioned here:

https://github.com/powershell/powershell/issues/621

And workaround which is mentioned in this thread is:

Get-ChildItem $somepath -Attributes ReparsePoint | % { $_.Delete() }

Or you can gather more information here:

https://github.com/PowerShell/PowerShell/pull/11331

Incomprehension answered 30/7, 2020 at 11:45 Comment(1)
@Quoth if you add -Recurse to Get-ChildItem, maybe.Lucey
V
2

There's always cmd. Wmi and powershell can't even delete profiles these days because of links.

cmd /c del bar
Vacuva answered 30/7, 2020 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.