kwizcom banner advertisement

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 which has subsites in it, SharePoint throws 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 few clicks:
  • Go to Site Settings >> Click on "Delete this site" link under site actions group.
  • Confirm the prompt once.
    sharepoint online delete subsite powershell
However, the problem is: When you have larger number of subsites (say: 50!) its cumbersome and time consuming process to delete each and every subsite using 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 given site which has no subsites underneath. Lets 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 another article for SharePoint On-Premises: How to Delete a Site with Subsites in SharePoint 2013 using PowerShell?
You can delete the Root level site (which may have subsites) without any problems, however!
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. 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. Reviewed by Salaudeen Rajack on 11:54 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.