Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete children directories in powershell including symlinks

Currently experiencing issue, I'm currently doing this:

Get-ChildItem $PATH -Recurse -ErrorAction SilentlyContinue | 
  Where-Object {($_.Attributes -notmatch '\"Directory\"') -and  
              ($_.LastWriteTime -lt (Get-Date).AddHours(-12))}| 
    Remove-Item -Force -Recurse

Now, it would delete fine IF I didn't have symlinks, but I do. I am getting this error:

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:184 + ... ($_.LastWriteTime -lt (Get-Date).AddHours(-12))}| Remove-Item -Force + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Remove-Item], Win32Exception + FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.RemoveItemCommand

I'm unable to upgrade powershell to v6. It seems to be related to:https://github.com/powershell/powershell/issues/621#issuecomment-289230180

Anyone have a workaround?

like image 205
Julian Alwandy Avatar asked Mar 06 '23 21:03

Julian Alwandy


2 Answers

Tested today on PowerShell 7 and unfortunately the issue is still there: PowerShell cannot delete symlink.

I had to delete the directory through an elevated prompt typing cmd /c rmdir /s /q C:\Users\Your_User_Name\Your_Folder_Name

like image 101
Francesco Mantovani Avatar answered Apr 26 '23 16:04

Francesco Mantovani


This problem seems to be fixed in PS 6 (ref: https://github.com/powershell/powershell/issues/621)

In PS 5.1 one can work around it by using:

$(get-item $theSymlinkDir).Delete()

or as LotPings noted in the comments to the Q for this specific Foreach-Object loop:

|? LinkType -eq 'SymbolicLink'| % { $_.Delete() }
like image 42
Martin Ba Avatar answered Apr 26 '23 15:04

Martin Ba