SharePoint Online: Delete a Subsite using PowerShell
Requirement: SharePoint Online PowerShell to Remove a Subsite.
How to Delete a Subsite in SharePoint Online?
Any user with Full Control or Manage Hierarchy permissions within a site can delete the site in SharePoint Online when its no longer needed. To delete a subsite from SharePoint Online:
SharePoint Online: How to delete a subsite?
Similarly, in modern sites, You can click on "Settings" >> Site Information >> Delete Site. Confirm the delete by selecting "Yes, delete this site and its associated content." checkbox and then click on "Delete" button. When you delete a site, you are also deleting any subsites, site content, and all data associated with the site, such as Site Settings, lists and documents libraries, etc.
Remove SharePoint Online Subsite using Sites and Workspaces:
You can also remove a subsite in SharePoint Online using "Sites and Workspaces" link. Here is how:
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:
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!
SharePoint Online PnP PowerShell to Remove Subsite
Here is how to delete subsite using PnP PowerShell.
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?
Here is the PnP PowerShell to delete all Subsites in SharePoint Online
If you accidentally delete a subsite, the site collection administrator can restore it from the site collection recycle bin. SharePoint Online: Restore Deleted Subsite from Recycle Bin using PowerShell
How to Delete a Subsite in SharePoint Online?
Any user with Full Control or Manage Hierarchy permissions within a site can delete the site in SharePoint Online when its no longer needed. 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.(If you don't see the "Delete this site" link, that means you don't have permission to delete the site!)
-
This leads you to the "Delete this site" page. Click on the "Delete" button and confirm subsite deletion.
- and you'll get "Your Web site has been deleted." message in a while.
SharePoint Online: How to delete a subsite?
Similarly, in modern sites, You can click on "Settings" >> Site Information >> Delete Site. Confirm the delete by selecting "Yes, delete this site and its associated content." checkbox and then click on "Delete" button. When you delete a site, you are also deleting any subsites, site content, and all data associated with the site, such as Site Settings, lists and documents libraries, etc.
Remove SharePoint Online Subsite using Sites and Workspaces:
You can also remove a subsite in SharePoint Online using "Sites and Workspaces" link. Here is how:
- Navigate to the parent site which contains the subsite you want to delete.
- Click on Settings Gear >> Select Site Settings
- On the Site Settings page, under Site Administration section, click on "Sites and Workspaces" link.
- On the Sites and Workspaces page, click on the Delete icon next to the subsite to be deleted.
- On the Delete This Site page, click the Delete button >> click the OK button to confirm.
You need to have SharePoint Online Management Shell installed on your machine before executing these scripts. 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 that 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.")
#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="[email protected]" $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() #sharepoint online powershell remove subsite $web.DeleteObject() $context.ExecuteQuery() Write-Host "SubSite Deleted:" $web.Title -foregroundcolor Green } catch{ write-host "Error: $($_.Exception.Message)" -foregroundcolor Red }This PowerShell removes SharePoint Online Subsite.
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="[email protected]" $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) } #sharepoint online powershell delete web $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 $SiteUrlThis PowerShell deletes all subsites in SharePoint Online site.
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 -ForceThe 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?
Here is the PnP PowerShell to delete all Subsites in SharePoint Online
#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
If you accidentally delete a subsite, the site collection administrator can restore it from the site collection recycle bin. SharePoint Online: Restore Deleted Subsite from Recycle Bin using PowerShell
Thanks for your help with this :)
ReplyDeleteIn 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
Updated! Thanks Mahmoud!!
Delete