Removing site, web and subwebs from SharePoint Online with PowerShell

We have all seen the error “There was a problem deleting Web site “/sites/RootSite/subweb1″. Sites that have subsites or certain apps can’t be deleted. Please try again after deleting all subsites and removing the apps.”

image

It could be that there is just 1 web site beneath the site you are about to remove but there could also be 1000 sub sites. I have created the below PowerShell script to automatically start removing site, web and subwebs from SharePoint Online with PowerShell.

Running the script

We will first start by opening the SharePoint Online Management Shell as administrator which can be downloaded at https://www.microsoft.com/en-us/download/details.aspx?id=35588.

You will need to change the first variables in the remove-SPOWebs function to match your Office 365 tenant and copy this bit to PowerShell.

function remove-SPOWeb ($web){
 #Uses the to be deleted variable as start point
 $context = New-Object Microsoft.SharePoint.Client.ClientContext($web)   
 $context.Credentials = $SPOCredentials
 $web = $context.Web 
 $context.Load($web)
 $context.Load($web.Webs)
 
 #send the request containing all operations to the server
 try{
  $context.executeQuery()
  
  #remove any subweb if present
  if ($web.Webs.Count -ne 0){
   foreach ($subweb in $web.Webs){
    remove-SPOWeb($subweb.url)
   }
  }
 
  Write-Host "Info: Removing $($web.url)" -foregroundcolor green
  $web.DeleteObject()
  $Context.ExecuteQuery()
 }
 catch{
  write-host "Error: $($_.Exception.Message)" -foregroundcolor green
 }
}
function remove-SPOWebs{
#variables that needs to be set before starting the script
$userName = "mpadmin@spfire.onmicrosoft.com"
$adminUrl = "https://spfire-admin.sharepoint.com"

# Let the user fill in the site that has to be deleted
$toBeDeleted = Read-Host "Please enter the full url of the site that needs to be deleted (e.g. https://spfire.sharepoint.com/sites/rootsite/subsite"

# Let the user fill in their password in the PowerShell window
$password = Read-Host "Please enter the password for $($userName)" -AsSecureString

# set SharePoint Online and Office 365 credentials
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $password

#connect to to Office 365
try{
Connect-SPOService -Url $adminUrl -Credential $credentials
write-host "Info: Connected succesfully to Office 365" -foregroundcolor green
}
catch{
write-host "Error: $($_.Exception.Message)" -foregroundcolor red
Break delete-SPOSites
}

#verify if site already exists in SharePoint Online
$siteExists = get-sposite | where{$_.url -eq $toBeDeleted}

#remove web if site isn't an site collection
if ($siteExists -eq $null) {
remove-SPOWeb($toBeDeleted)
}
else {
Remove-SPOSite -Identity $siteExists -NoWait -confirm$false
write-host "Info: The site collection $($toBeDeleted) has been removed" -foregroundcolor green
}
}
remove-SPOWebs

image

first enter the URL that will be the starting point.

image

Next fill in the password for the admin account

image

It will first remove the sub sites and then makes its way to the start site.

Leave a Reply

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