How to Delete Subsite in SharePoint using PowerShell?
How to delete a subsite in SharePoint 2013 / 2016?
If you’re administering a SharePoint site, there may come a time when you need to delete a subsite. This can be done in just a few easy steps. This blog post will show you how to delete a subsite in SharePoint. We’ll also show you how to use PowerShell to delete a subsite!
To delete a subsite in SharePoint 2013, Navigate to Site Settings >> Click on the “Delete this site” link under the “Site Actions” group.
That takes you to the “Delete this site” page. Click on the “Delete” button and confirm site deletion.
So simple, huh?
Delete all subsites in SharePoint using PowerShell
Why do we need to use PowerShell while deleting subsites from SharePoint 2013 web UI is relatively simpler? Well, If you try to delete a subsite that has its subsites, You’ll end up in Error!
SharePoint doesn’t allow you to delete a site with its sub-sites (But You can delete a site collection and all its subsites in a single click from web UI by following the above instructions!). In other words, You cannot delete a SharePoint Subsite with one or more child subsites. You’ll have to individually remove each and every child subsite prior to removing the parent subsite.
Sorry, something went wrong
There was a problem deleting Web site “/Subsite”. Sites that have subsites or certain apps can’t be deleted. Please try again after deleting all subsites and removing the apps.
So, to delete a site with subsites in SharePoint, first, we have to delete all child subsites and then delete the parent site. What if you have a large number of subsites? Well, here is where PowerShell comes to the rescue.
Using content and structure is one way as in Delete Site with Subsites in SharePoint – Quick Way, It needs Publishing feature to be turned on. So, let’s utilize PowerShell to delete subsite in SharePoint 2013.
Delete a Subsite using PowerShell:
$SiteURL = "https://intranet.crescent.com/sites/operations/us"
#Get the site
$web = Get-SPWeb $SiteURL
#Delete the subsite
$web.Delete()
Delete all subsites in SharePoint 2013 using PowerShell (Delete All Child Webs)
To remove a subsite in SharePoint 2013, use this PowerShell script. It basically scans and removes all subsites recursively.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Function Remove-ChildSites([Microsoft.SharePoint.SPWeb]$Web)
{
Foreach($ChildWeb in $Web.Webs)
{
#Call the function recursively TO DELETE all sub-childs
Remove-ChildSites($ChildWeb)
}
Write-host Removing web $Web.Url
#Remove the web
Remove-SPWeb $Web -Confirm:$false
}
$ParentWebURL="https://intranet.crescent.com/sites/operations/us"
#Get the Parent Web
$ParentWeb= Get-SPWeb $ParentWebURL
#Call the function to remove all child webs
Remove-ChildSites $ParentWeb
I didn’t know about the subsite problem – glad I checked here first. Thank you!
Very nice script -thank you