Skip to content

Instantly share code, notes, and snippets.

@lamchau
Created February 26, 2016 05:00
Show Gist options
  • Save lamchau/fedd26a438cd7b2a124b to your computer and use it in GitHub Desktop.
Save lamchau/fedd26a438cd7b2a124b to your computer and use it in GitHub Desktop.
multithreaded large folder delete on windows
# for deleting large directories (e.g. node_modules)
function purge($target_dir) {
if (Test-Path -Path $target_dir) {
$uuid = [guid]::NewGuid().toString()
$empty_dir = "$env:temp$uuid.tmp"
New-Item -ItemType directory -Path $empty_dir | Out-Null
# delete using robocopy /NFL ... (options to suppress stdout)
robocopy $empty_dir $target_dir /MIR /MT | Out-Null
Remove-Item $empty_dir | Out-Null
$target_dir = (Resolve-Path $target_dir).Path
Remove-Item -Recurse -Force $target_dir | Out-Null
Write-Output "Deleted: $target_dir"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment