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.

delete subsite from sharepoint online

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:

  1. Navigate to the Subsite >> Go to Site Settings >> Click on the “Delete this site” link under the site actions group.
  2. Confirm the prompt once.
    sharepoint online delete subsite powershell

However, when you have a larger number of subsites (say: 50!), deleting every subsite using the browser interface is cumbersome and time-consuming. 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 my other article on deleting subsites in SharePoint Online: How to Delete Subsites in SharePoint Online using PowerShell?

You can delete the Root level site (which may have subsites) without any problems, however!

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *