It can be very time consuming in the SharePoint browser UI to delete a large structure of sub-sites below a particular site. This is because you are unable to delete a site that contains other sub-sites without deleting those sub-sites first, which in turn may contain other sub-sites!
The PowerShell script below allows you to choose a start site (i.e., the site from which you want to delete all sub-sites), walks through each sub-site and deletes it, starting from sites at the end of the hierarchy and working backwards until it returns back to the start site, which it will not delete.
First, run the PowerShell script below – Note: Doing this will not start deleting sites as you have to call the functions in the script with a separate command (see below).
function Delete-SPSubSites ($StartSite)
#Get the starting site from which to delete all sub-sites
$web = Get-SPWeb $StartSite
#Run function to get all sub-sites below the start site
Get-SPSubWebs -StartWeb $web
function Get-SPSubWebs ($StartWeb)
#Get all sub-sites below the start site
$webs = $StartWeb.GetSubwebsForCurrentUser()
#Run this routine if there are sub-sites present
if ($webs.Count -gt 0)
#Walk through each sub-site
foreach ($nextWeb in $webs)
#Check for sub-sites underneath this site
Get-SPSubWebs -StartWeb $nextWeb
#Run function to delete the site
Delete-SPSubWeb -DeleteWeb $nextWeb
function Delete-SPSubWeb ($DeleteWeb)
#Delete the site specified in the $DeleteWeb variable
write-host "Deleting site"$DeleteWeb.Title"at"$DeleteWeb.Url
Once you have run the script, you can use it to delete sub-sites by typing the following command in the PowerShell console:
Delete-SPSubSites -StartSite http://portal/team
The example above will delete all sub-sites below http://portal/team, but will not delete the http://portal/team site itself.
In case it’s not obvious (!), be very careful with the use of this script. Take a backup of your site collection first, and run on a development server before using this script in a live environment to ensure you delete the correct sites.