There was a problem deleting Web site “Site-URL”. Sites that have subsites or certain apps can’t be deleted. Please try again after deleting all subsites and removing the apps.
Problem: If you try to delete a SharePoint Online site that has subsites in it, SharePoint throws an error message saying
“There was a problem deleting Web site “<subsite-url>”. Sites that have subsites or certain apps can’t be deleted. Please try again after deleting all subsites and removing the apps.“
Solution:
So basically, SharePoint Online tells us: You have to delete all subsites first in order to delete its parent site. Deleting a subsite is a matter of a few clicks:
- Navigate to the Subsite >> Go to Site Settings >> Click on “Delete this site” link under site actions group.
- Confirm the prompt once.
However, the problem is: When you have a larger number of subsites (say: 50!), it’s a cumbersome and time-consuming process to delete each and every subsite using the browser interface. So, the solution is PowerShell!
PowerShell to delete a subsite in SharePoint Online:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Set Parameters
$SiteURL="https://crescent.sharepoint.com/sites/Ops/us/2017"
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
$Ctx.web.DeleteObject()
$Ctx.ExecuteQuery()
write-host -f Green "Subsite Deleted Successfully "$SiteURL
This PowerShell script deletes the given site, which has no subsites underneath. Let’s add some error handling code and wrap it inside a reusable function. The below PowerShell script recursively deletes a site and all of its subsites.
SharePoint Online: Delete a Site with All Its Subsites using PowerShell:
Here is the PowerShell script to delete all subsites from a SharePoint Online site
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Custom Function to Delete a site with subsites
Function SPODelete-Subsite ($SiteURL)
{
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
$web = $Ctx.Web
$Ctx.Load($web)
#Get all subsites under given site
$Ctx.Load($web.Webs)
$Ctx.ExecuteQuery()
#Loop through each subsite in the current web
foreach ($SubWeb in $web.Webs)
{
#Call the function recursively to delete all subsites under current site
SPODelete-Subsite($Subweb.url)
}
#Delete the subsite
$web.DeleteObject()
$Ctx.ExecuteQuery()
write-host -f Green "Subsite Deleted Successfully "$SiteURL
}
Catch {
write-host -f Red "Error Deleting Subsite $($SiteURL)" $_.Exception.Message
}
}
#Set Parameters
$SiteURL="https://crescent.sharepoint.com/sites/Ops/us"
#Call the function to delete site with subsites
SPODelete-Subsite -SiteURL $SiteURL
Here is another article for SharePoint On-Premises: How to Delete a Site with Subsites in SharePoint 2013 using PowerShell?