How to recursively delete an entire directory with PowerShell 2.0?

前端 未结 18 2092
南旧
南旧 2020-12-07 07:26

What is the simplest way to forcefully delete a directory and all its subdirectories in PowerShell? I am using PowerShell V2 in Windows 7.

I have learned from severa

相关标签:
18条回答
  • 2020-12-07 07:39

    There seems to be issues where Remove-Item -Force -Recurse can intermittently fail on Windows because the underlying filesystem is asynchronous. This answer seems to address it. The user has also been actively involved with the Powershell team on GitHub.

    0 讨论(0)
  • 2020-12-07 07:40

    Use the old-school DOS command:

    rd /s <dir>
    
    0 讨论(0)
  • 2020-12-07 07:40
    del <dir> -Recurse -Force # I prefer this, short & sweet
    

    OR

    remove-item <dir> -Recurse -Force
    

    If you have a huge directory then what I usually do is

    while (dir | where name -match <dir>) {write-host deleting; sleep -s 3}
    

    Run this on another powershell terminal and it will stop when it is done.

    0 讨论(0)
  • 2020-12-07 07:43

    Deleting an entire folder tree sometimes works and sometimes fails with "Directory not empty" errors. Subsequently attempting to check if the folder still exists can result in "Access Denied" or "Unauthorized Access" errors. I do not know why this happens, though some insight may be gained from this StackOverflow posting.

    I have been able to get around these issues by specifying the order in which items within the folder are deleted, and by adding delays. The following runs well for me:

    # First remove any files in the folder tree
    Get-ChildItem -LiteralPath $FolderToDelete -Recurse -Force | Where-Object { -not ($_.psiscontainer) } | Remove-Item –Force
    
    # Then remove any sub-folders (deepest ones first).    The -Recurse switch may be needed despite the deepest items being deleted first.
    ForEach ($Subfolder in Get-ChildItem -LiteralPath $FolderToDelete -Recurse -Force | Select-Object FullName, @{Name="Depth";Expression={($_.FullName -split "\\").Count}} | Sort-Object -Property @{Expression="Depth";Descending=$true}) { Remove-Item -LiteralPath $Subfolder.FullName -Recurse -Force }
    
    # Then remove the folder itself.  The -Recurse switch is sometimes needed despite the previous statements.
    Remove-Item -LiteralPath $FolderToDelete -Recurse -Force
    
    # Finally, give Windows some time to finish deleting the folder (try not to hurl)
    Start-Sleep -Seconds 4
    

    A Microsoft TechNet article Using Calculated Properties in PowerShell was helpful to me in getting a list of sub-folders sorted by depth.

    Similar reliability issues with RD /S /Q can be solved by running DEL /F /S /Q prior to RD /S /Q and running the RD a second time if necessary - ideally with a pause in between (i.e. using ping as shown below).

    DEL /F /S /Q "C:\Some\Folder\to\Delete\*.*" > nul
    RD /S /Q "C:\Some\Folder\to\Delete" > nul
    if exist "C:\Some\Folder\to\Delete"  ping -4 -n 4 127.0.0.1 > nul
    if exist "C:\Some\Folder\to\Delete"  RD /S /Q "C:\Some\Folder\to\Delete" > nul
    
    0 讨论(0)
  • 2020-12-07 07:46

    Try this example. If the directory does not exist, no error is raised. You may need PowerShell v3.0.

    remove-item -path "c:\Test Temp\Test Folder" -Force -Recurse -ErrorAction SilentlyContinue
    
    0 讨论(0)
  • 2020-12-07 07:49
    Remove-Item -Recurse -Force some_dir
    

    does indeed work as advertised here.

    rm -r -fo some_dir
    

    are shorthand aliases that work too.

    As far as I understood it, the -Recurse parameter just doesn't work correctly when you try deleting a filtered set of files recursively. For killing a single dir and everything below it seems to work fine.

    0 讨论(0)
提交回复
热议问题