Skip to content

Remove-Item: adding -Recurse when -Filter is also used ignores the filter, targets *all* items. #20651

Closed

Description

Prerequisites

Steps to reproduce

This is a serious problem that can result in unwanted deletion of both files and subdirectories.

Related:

# Create and switch to a temp. dir.
Push-Location ($tmpDir = New-Item -Force -Type Directory (Join-Path temp: $PID))

# Create two sample files and a sample subdir.
'1.txt', '2.csv' | % { '' > $_ }
$null = New-Item -Force -Type Directory subdir

# Use -Filter *.txt alone 
Remove-Item * -Filter *.txt -WhatIf

'---'

# Use -Filter *.txt with -Recurse
Remove-Item * -Filter *.txt -Recurse -WhatIf

# Clean up
Pop-Location
$tmpDir | Remove-Item -Recurse 

Expected behavior

What if: Performing the operation "Remove File" on target "/var/folders/_t/8x3_gp2n6lb1mpg106v6vsvc0000gn/T/64474/1.txt".
---
What if: Performing the operation "Remove File" on target "/var/folders/_t/8x3_gp2n6lb1mpg106v6vsvc0000gn/T/64474/1.txt".

Actual behavior

What if: Performing the operation "Remove File" on target "/var/folders/_t/8x3_gp2n6lb1mpg106v6vsvc0000gn/T/64474/1.txt".
---
What if: Performing the operation "Remove Directory" on target "/var/folders/_t/8x3_gp2n6lb1mpg106v6vsvc0000gn/T/64474/subdir".
What if: Performing the operation "Remove File" on target "/var/folders/_t/8x3_gp2n6lb1mpg106v6vsvc0000gn/T/64474/1.txt".
What if: Performing the operation "Remove File" on target "/var/folders/_t/8x3_gp2n6lb1mpg106v6vsvc0000gn/T/64474/2.csv".

That is, all items were targeted when -Recurse was added

Error details

No response

Environment data

PowerShell 7.4.0-rc.1

Visuals

No response

Activity

  1. changed the title [-]`Remove-Item`: adding `-Recurse` when `-Filter` is also used ignores the filter[/-] [+]`Remove-Item`: adding `-Recurse` when `-Filter` is also used ignores the filter, targets *all* items.[/+] on Nov 7, 2023
  2. added
    WG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.
    WG-NeedsReviewNeeds a review by the labeled Working Group
    on Nov 7, 2023
  3. SeeminglyScience commented on Apr 15, 2024

    @SeeminglyScience
    Contributor

    The Engine WG discussed this. While we agree that it is a bug and would ideally work more like -Include does, we are worried about making a change in this particularly sensitive area and are marking this as wontfix.

  4. added
    WG-ReviewedA Working Group has reviewed this and made a recommendation
    Resolution-Won't FixThe issue won't be fixed, possibly due to compatibility reason.
    and removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    WG-NeedsReviewNeeds a review by the labeled Working Group
    on Apr 15, 2024
  5. microsoft-github-policy-service commented on Apr 16, 2024

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as won't fix and has not had any activity for 1 day. It has been closed for housekeeping purposes.

  6. microsoft-github-policy-service commented on Apr 16, 2024

    @microsoft-github-policy-service
    Contributor

    📣 Hey Michael Klement (@mklement0), how did we do? We would love to hear your feedback with the link below! 🗣️

    🔗 https://aka.ms/PSRepoFeedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-Won't FixThe issue won't be fixed, possibly due to compatibility reason.WG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.WG-ReviewedA Working Group has reviewed this and made a recommendation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions