How to Delete Subsite in SharePoint using PowerShell?

How to delete a subsite in SharePoint 2013?

To delete a subsite in SharePoint 2013, Navigate to Site Settings >> Click on the “Delete this site” link under the “Site Actions” group. sharepoint 2013 powershell remove subsite
That takes you to the “Delete this site” page. Click on the “Delete” button and confirm site deletion. 

delete subsite sharepoint 2013 powershell

So simple, huh?

To restore deleted subsite in SharePoint 2010(sp1) or in SharePoint 2013, Just go to your Site collection recycle bin!

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 own 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 above instructions!). In other words You cannot delete SharePoint Subsite with one or more child subsites. You’ll have to individually remove each and every child subsite prior removing 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.
cannot delete sharepoint subsite
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 = "http://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="http://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

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

2 thoughts on “How to Delete Subsite in SharePoint using PowerShell?

  • July 1, 2020 at 1:53 PM

    I didn’t know about the subsite problem – glad I checked here first. Thank you!

    Reply
  • May 23, 2018 at 6:57 AM

    Very nice script -thank you

    Reply

Leave a Reply