Repository navigation
Description
Update: Based on Andrew Stanton (@StingyJack)'s feedback, part of this issue may be related to a longstanding known Remove-Item problem currently documented as:
When it is used with the
Includeparameter, theRecurseparameter might not delete all subfolders or all child items. This is a known issue.
Get-ChildItem allows the following idioms to look for matching files on all levels of the target directory's hierarchy:
Get-ChildItem *.txt -Recurse
Get-ChildItem . -Recurse -Include *.txtBy contrast, these idioms currently do not work with Remove-Item:
Steps to reproduce
Set-Location ~
Get-ChildItem *.txt -Recurse
Get-ChildItem . -Recurse -Include *.txt
Remove-Item *.txt -Recurse -WhatIf
Remove-Item . -Recurse -Include *.txt -WhatIfExpected behavior
All 4 commands should output / target the same set of files, across the directory hierarchy.
Actual behavior
The Get-ChildItem commands behave as expected.
The Remove-Item commands do not:
-
Remove-Item *.txt -Recurse -WhatIfonly targets items directly in the target directory (does not recurse)- This behavior is sensible, but it deviates from
Get-ChildItem's behavior - see Get-ChildItem -Recurse treats literals passed to -Path like wildcard expressions #5699
- This behavior is sensible, but it deviates from
-
Remove-Item . -Recurse -Include *.txt -WhatIffails.
E.g., on macOS:Remove-Item : Cannot remove the item at '/Users/jdoe' because it is in use.; i.e., an attempt is made to blindly remove., without considering the-Includewildcard pattern.- Note that the problem does not occur if you target a directory by name or path, from a different directory (rather than with
.from inside it).
- Note that the problem does not occur if you target a directory by name or path, from a different directory (rather than with
Environment data
PowerShell Core v6.0.0-alpha (v6.0.0-alpha.18) on macOS 10.12.4
PowerShell Core v6.0.0-alpha (v6.0.0-alpha.18) on Microsoft Windows 10 Pro (64-bit; v10.0.14393)
Activity
StingyJack commentedon Feb 8, 2018
Remove-Item also does not appear to either sort the items for deletion by leaf-first or at least check if the item exists before deleting it.
The example pattern is to use Get-ChildItem and pipe the results
but this will get "directory is not empty" and other errors, and you have to run the same thing 2 or more times to actually clean the folder out.
Its also does not carry over the behavior of
rd /sorrmdirwhere a directory and all its contents must be removed. These two causes lead to code like the following being necessary to have a better chance (but not guarantee) of working.[-]Inconsistencies between Get-ChildItem and Remove-Item with respect to -Recurse[/-][+]Inconsistencies between Get-ChildItem and Remove-Item with respect to -Recurse and -Include[/+]mickaeltr commentedon May 13, 2020
Hello Andrew Stanton (@StingyJack)
It seems that the issue is bigger than that (prior to Windows 10 1903, if I understood correctly)
This video seems to explain well the bug: https://www.youtube.com/watch?v=uhRWMGBjlO8&feature=youtu.be&t=455
And someone made a terrifying script 🤯 about it: https://stackoverflow.com/questions/53553729/cannot-remove-item-the-directory-is-not-empty/53561052#53561052
mklement0 commentedon May 13, 2020
Mickaël Tricot (@mickaeltr) Your referring to #8211, which will ultimately only be resolved once the WinAPI file-removal calls become synchronous (you can use the function as a workaround, but it has limitations).
mickaeltr commentedon May 13, 2020
Thanks Michael Klement (@mklement0) for the link and explanation.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.