Delete SharePoint sites recursively
Today I tried to delete a SharePoint site (SPWeb) with child sites inside. I tried to delete it form administrator interface but I got an error. Something was wrong with templates or site structure. So I decided to use PowerShell for SharePoint.
I entered "Remove-SPWeb http://spdev/folder/" and pressed enter, but I got an error that this site has child sites and cannot be deleted.
So I decided to write a PowerShell script for removing:
function RemoveSPWebRecursively( [Microsoft.SharePoint.SPWeb] $web) { Write-Debug "Removing site ($($web.Url))..." $subwebs = $web.GetSubwebsForCurrentUser() foreach($subweb in $subwebs) { RemoveSPWebRecursively($subweb) $subweb.Dispose() } $DebugPreference = "SilentlyContinue" Remove-SPWeb $web -Confirm:$false $DebugPreference = "Continue" } $DebugPreference = "SilentlyContinue" $web = Get-SPWeb "http://spdev/folder/" $DebugPreference = "Continue" If ($web -ne $null) { RemoveSPWebRecursively $web $web.Dispose() }