kwizcom banner advertisement

SharePoint Online: Delete a Subsite using PowerShell

Requirement: SharePoint Online PowerShell to Remove Subsite

How to Delete a Subsite in SharePoint Online?
To delete a subsite from SharePoint online:
  • Click on Site Settings gear >> Choose "Site Settings".
  • Click on "Delete this Site" link under "Site Actions" group.
    sharepoint online powershell delete subsite
  • This leads you to the "Delete this site" page. Click on "Delete" button and confirm subsite deletion.
    delete a subsite sharepoint online
  • and you'll get "Your Web site has been deleted." message in a while.
This removes the subsite in SharePoint online. Lets see how to delete subsite in SharePoint Online using PowerShell!
You need to have SharePoint Online Management Shell installed on your machine before executing these script. You can download it from here http://www.microsoft.com/en-in/download/details.aspx?id=35588

Delete a subsite in SharePoint Online using PowerShell:
How to delete a subsite in SharePoint Online using PowerShell? Well, while removing subsites in SharePoint Online using web browser UI is relatively simpler, We may need PowerShell in certain cases, such as:
  • When you have to delete subsites in bulk.
  • If you want to delete a sub-site which has its child-subsites (You'll get this error: "There was a problem deleting Web site "/sites/Sales/apac". Sites that have subsites or certain apps can't be deleted. Please try again after deleting all subsites and removing the apps.")
Here is the PowerShell to delete a subsite in SharePoint online.
#Load SharePoint Online 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"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/apac/"
$UserName="Salaudeen@crescent.com"
$Password ="Password goes here"

try{  
    #Setup Credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

    $context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $context.Credentials = $credentials
    
    #get the subsite
    $web = $context.Web
    $context.Load($web)
    $context.ExecuteQuery()

    #Delete subsite
    $web.DeleteObject()
    $context.ExecuteQuery()

    Write-Host "SubSite Deleted:" $web.Title -foregroundcolor Green
}
catch{
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

How to Delete All subsites under a Site in SharePoint Online using PowerShell:
Let's wrap the above script into a reusable function to delete subsites recursively in a site collection!
#Load SharePoint Online 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"
   
#Variables for Processing
$SiteUrl = "https://portal.sharepoint.com/sites/Sales/apac/us/"
$UserName="Salaudeen@crescent.com"
$Password ="password goes here"

#Function to delete a subsite and its all child sites
Function Remove-SPOWeb() {
param(
    $WebURL = $(throw "Please Enter the Subsite URL to Remove:")
)
    try{
        #Setup Credentials to connect
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
  
        #Get Web information and subsites
        $Context = New-Object Microsoft.SharePoint.Client.ClientContext($webURL)
        $Context.Credentials = $credentials
        #get the subsite
        $web = $context.Web
        $context.Load($web)
        $Context.Load($web.Webs)
        $Context.executeQuery() 
 
        #Iterate through each subsite in the current web
        foreach ($Subweb in $web.Webs)
        {
            #Call the function recursively to process all subsites underneaththe current web
            Remove-SPOWeb($Subweb.url)
        }

        #Delete subsite
        $web.DeleteObject()
        $context.ExecuteQuery()
        Write-Host "SubSite Deleted:" $web.Title -foregroundcolor Green
        }
    catch{
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
}
 
#Call the function to remove subsite
Remove-SPOWeb -WebURL $SiteUrl

SharePoint Online PnP PowerShell to Remove Subsite 
Here is how to delete subsite using PnP PowerShell.
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/Sales"
$SubsiteURL = "2018" # Just URL of the Subsite under parent site - NOT Relative Path

#Get Credentials to connect
$Cred = Get-Credential
 
#Connect PNP Online to Parent Web
Connect-PnPOnline -Url $SiteURL -Credentials $Cred

#sharepoint online delete subsite powershell
Remove-PnPWeb -Url $SubsiteURL -Force
The Remove-PnPWeb cmdlet permanently removes a specific subsite. However, if your subsite has any child subsites, the script will end up in error saying:
Remove-PnPWeb : There was a problem deleting Web site "/sales". Sites that have subsites or certain apps can't be deleted. Please try again after deleting all subsites and removing the apps.

How to Delete a Site with all its subsites using PnP PowerShell?
#Function to delete a subsite and its all child sites
Function Remove-PnPAllWebs([Microsoft.SharePoint.Client.Web]$Web) 
{
    #Get All Subsites of the web
    $SubWebs  = Get-PnPSubWebs -Identity $Web
    ForEach($SubWeb in $SubWebs)
    {
        #Call the function recursively to remove subsites
        Remove-PnPAllWebs($SubWeb)
    }
    #Delete the subsite
    Remove-PnPWeb -Identity $Web -Force
    Write-host -f Green "Deleted Sub-Site:"$Web.URL
}

#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sales"

#Get Credentials to connect
$Cred = Get-Credential
 
#Connect PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred

#Get the Web 
$Web = Get-PnPWeb

#sharepoint online delete subsite powershell
Remove-PnPAllWebs $Web
SharePoint Online: Delete a Subsite using PowerShell SharePoint Online: Delete a Subsite using PowerShell Reviewed by Salaudeen Rajack on May 29, 2016 Rating: 5

2 comments:

  1. Thanks for your help with this :)

    In the second script, add this line after try{
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

    Otherwise it will get error for not authenticating.

    Thanks
    Mahmoud

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.