Skip to content

Instantly share code, notes, and snippets.

@evenkiel
Created June 25, 2012 23:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evenkiel/2992175 to your computer and use it in GitHub Desktop.
Save evenkiel/2992175 to your computer and use it in GitHub Desktop.
Powershell function to recursively delete a folder and all of its child folders and files
#
# Given a root directory, perform a depth first recursive delete of all subdirectories and files.
# Necessary b/c for some reason powershell won't always succeed in a recursive delete of folders which contain
# subfolders
#
function RecursiveDelete($theroot) {
$children = Get-ChildItem -Path $theroot | where-object { $_.Attributes -eq "Directory"} |% {$_.FullName}
foreach($achild in $children) {
if ($achild -ne $null) {
RecursiveDelete $achild
}
}
Remove-Item $theroot -recurse -force
}
@stanislavhordiyenko
Copy link

Thank you for this gist!

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