Sunday, May 29, 2016

SharePoint Online: Delete a Subsite using PowerShell

How to delete subsite in SharePoint online?
To delete a subsite 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.
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:
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:
Lets wrap the above script into a reusable function to delete subsites recursively!
#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{  
        #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



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...