Skip to content

Remove-Item throws The directory is not empty when deleting large file trees #27710

Open

Description

Prerequisites

Steps to reproduce

> 1..100 | % { New-Item -Type Directory -Force "C:\Temp\repro\$_\$(1..100 -join '\')" | Out-Null }
> Remove-Item "C:\Temp\repro" -Recurse -Force

Expected behavior

`C:\Temp\repro` is removed

Actual behavior

Remove-Item: The directory is not empty. : 'C:\Temp\repro\1\1\2\3\4\5\6'.
Remove-Item: The directory is not empty. : 'C:\Temp\repro\1\1\2\3\4\5'.
Remove-Item: The directory is not empty. : 'C:\Temp\repro\1\1\2\3\4'.
Remove-Item: The directory is not empty. : 'C:\Temp\repro\1\1\2\3'.
Remove-Item: The directory is not empty. : 'C:\Temp\repro\1\1\2'.
Remove-Item: The directory is not empty. : 'C:\Temp\repro\1\1'.
Remove-Item: The directory is not empty. : 'C:\Temp\repro\1'.
Remove-Item: The directory is not empty. : 'C:\Temp\repro\10\1\2\3\4\5\6\7\8\9\10\11\12\13\14\15\16\17\18\19\20\21\22\23\24\25\26\27\28\29\30\31\32\33\34\35\36\37\38\39\40\41\42\43\44\45\46\47\48\49\50\51\52\53\54\55\56\57\58\59\60\61'.
... and etc.

Error details

> get-error

Exception             :
    Type       : System.IO.IOException
    TargetSite :
        Name          : RemoveDirectoryInternal
        DeclaringType : [System.IO.FileSystem]
        MemberType    : Method
        Module        : System.Private.CoreLib.dll
    Message    : The directory is not empty. : 'C:\Temp\repro'.
    Source     : System.Private.CoreLib
    HResult    : -2147024751
    StackTrace :
   at System.IO.FileSystem.RemoveDirectoryInternal(String fullPath, Boolean topLevel, Boolean allowDirectoryNotEmpty)
   at System.IO.DirectoryInfo.Delete()
   at Microsoft.PowerShell.Commands.FileSystemProvider.RemoveFileSystemItem(FileSystemInfo fileSystemInfo, Boolean force)
TargetObject          : C:\Temp\repro
CategoryInfo          : WriteError: (C:\Temp\repro:DirectoryInfo) [Remove-Item], IOException
FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
ErrorDetails          :
InvocationInfo        :
    MyCommand        : Remove-Item
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 36
    Line             : Remove-Item "C:\Temp\repro" -Recurse -Force -ProgressAction Continue
    Statement        : Remove-Item "C:\Temp\repro" -Recurse -Force -ProgressAction Continue
    PositionMessage  : At line:1 char:1
                       + Remove-Item "C:\Temp\repro" -Recurse -Force -ProgressAction Continue
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Remove-Item
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :
      0
      1

Environment data

Name                           Value
----                           -----
PSVersion                      7.6.2
PSEdition                      Core
GitCommitId                    7.6.2
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.4
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Remove-Item "C:\Temp\repro" -Recurse -Force -ProgressAction SilentlyContinue does not help, only $ProgressPreference = 'SilentlyContinue' works.

Guess this is somewhat dependant on the storage performance / load on the box, manged to repro it only on some machines.

Process Monitor shows GetTotalFiles worker thread scanning the file tree while the pipeline thread tries to SetDispositionInformationFile Delete: True but gets back NOT EMPTY

Activity

  1. added
    Needs-Triage
    on Jul 22, 2026
  2. added
    WG-NeedsReview
    WG-Engine
    on Jul 28, 2026
  3. mklement0 commented on Aug 7, 2026

    @mklement0
    Contributor

    AI tells me that Microsoft Windows 10.0.17763 (from your environment data) implies "Windows 10 version 1809 (the "October 2018 Update")."

    This would explain the asynchronous behavior, as it predates the fix mentioned in the initial post of #8211 (Windows 10 version 1909 or later)

    Indeed, I cannot reproduce your symptom on PowerShell Core v7.6.4 (.NET 10.0.10) on Microsoft Windows 11 Pro (ARM 64-bit Processor; Version 25H2, OS Build: 26200.8737)

  4. ajansveld commented on Aug 7, 2026

    @ajansveld

    Good catch, this is in fact Windows Server 2019 (supported until 2029, so this definitely needs a fix).

  5. mklement0 commented on Aug 7, 2026

    @mklement0
    Contributor

    Since Windows 10 and Windows Server 2019 share the same foundation and given how old Windows 10 version 1809 is, I strongly suspect (but don't know for sure) that Windows Server 2019 has since seen updates in which the problem is fixed.

  6. ajansveld commented on Aug 17, 2026

    @ajansveld

    Since Windows 10 and Windows Server 2019 share the same foundation and given how old Windows 10 version 1809 is, I strongly suspect (but don't know for sure) that Windows Server 2019 has since seen updates in which the problem is fixed.

    Which problem? Apparently not this one, the hosts in question are fully patched.

  7. mklement0 commented on Aug 17, 2026

    @mklement0
    Contributor

    The problem of the file system-item deletion system APIs having historically been inherently asynchronous, by (problematic) design.

    Speaking in no official capacity whatsoever, my hunch is:

    Given the age of the affected platform and given that the problem has been resolved in more recent versions, it is highly unlikely that action will be taken, especially on the PowerShell side (compensating for potentially undesired behavior of system-level APIs is not usually done).

  8. mklement0 commented on Aug 17, 2026

    @mklement0
    Contributor

    P.S.: If you want a - suboptimal - workaround, see this Stack Overflow answer.

  9. Tadas commented on Aug 20, 2026

    @Tadas
    ContributorAuthor

    Just to be clear about the environment, this is on Server 2019 with August 11 2026—KB5120238 (OS Build 17763.9121) patch. Whatever the exact history on the OS side, this build clearly doesn't have the newer delete behaviour (which is fine).

    With that said, this is very clearly a PowerShell regression introduced in 7.5 by the Remove-Item progress bar:

    • 7.4 on the same host, same tree: no failures in at least 10 runs.
    • 7.6 on the same host, same tree: failures every time.
    • 7.6 on the same host with $ProgressPreference = 'SilentlyContinue': no failures.

    To me feels like this is getting lost in the thread:

    On top of that, the fact that -ProgressAction isn't respected is another problem with the implementation.

  10. mklement0 commented on Aug 20, 2026

    @mklement0
    Contributor

    Whatever the exact history on the OS side, this build clearly doesn't have the newer delete behaviour (which is fine).

    I don't think it is fine: you'll still see failures - even if less frequently so than with the 7.5+ progress bar in the picture (I'm trusting your analysis there).

    To put it differently: the only robust fix is to either:

    • (a) fix the system APIs by making them synchronous - which is true since at least Windows 11 (as well as Windows 10 versions 1909 or later) and - if I were to guess - since at least Windows Server 2022.

    • (b) compensate for the system API behavior on the PowerShell side.

    As noted before, (b) strikes me as highly unlikely, as does retrofitting the new synchronous behavior at the level of the system calls to Windows Server 2019.

  11. Tadas commented on Aug 21, 2026

    @Tadas
    ContributorAuthor

    Oh yeah I see your point, I was too focused on this particular scenario and forgot that something else could have a handle open.

    I don't think it is a pass to make it worse, though. Again, Server 2019 is still supported and we've not hit the issue in 7.4 (which annoyingly is about to hit end-of-support).

    (b) strikes me as highly unlikely

    I share this sentiment. But it doesn't seem like "new" issues are likely to get looked at either.

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

    Needs-TriageWG-EngineWG-NeedsReview

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions