SharePoint Online: Delete Site Collection using PowerShell

Requirement: Delete SharePoint online Site Collection using PowerShell.

How to delete a site collection in SharePoint Online?
SharePoint Online site collection is a collection of one or more sites that are grouped under the common URL and typically share a common navigation, branding, features, security, etc. Deleting a site collection is a quite straightforward process. To delete a SharePoint Online site collection from the Admin center, follow the below steps:
  • Login to your SharePoint Online Admin Center (Typically at https://tenant-admin.sharepoint.com)
  • Click on Sites >> Active Sites in the left navigation. From the site collections list, select the site collection to delete. From the Toolbar, click on "Delete" and then confirm by a prompt by clicking the "Delete" button.
    delete sharepoint online site using powershell
If you selected an Office 365 group connected site, you'll get a prompt with an option to delete the associated Office 365 group as well. Deleted sites are retained for 93 days in SharePoint Online, and then they will be permanently deleted. However, the deleted Office 365 groups are retained for only 30 days in Azure AD Recycle Bin.

Delete Multiple Site Collections in SharePoint Online
Similarly, You can delete multiple SharePoint Online sites using the modern admin center by selecting more than one site collections >> Click on "Bulk Edit" >> Choose "Delete" item in the menu and confirm the prompt!
delete multiple site collections in sharepoint online

Once deleted, You can find them under the "Deleted sites" link from the SharePoint Online admin center's left navigation. Now, let's delete a site collection in SharePoint Online with PowerShell!

How to Delete a SharePoint Online Site Collection using PowerShell?
Here is the PowerShell script for SharePoint online to delete site collection. Use SharePoint Online Management Shell or PowerShell ISE to run these cmdlets.
Import-Module Microsoft.Online.Sharepoint.PowerShell

#Variables for processing
$AdminSiteURL = "https://crescent-admin.sharepoint.com/"
$AdminName = "[email protected]"
$SiteCollURL = "https://crescent.sharepoint.com/sites/Sales"

#Credentials to connect 
$SecurePWD = ConvertTo-SecureString "Password" –asplaintext –force  
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $AdminName, $SecurePWD
 
#Connect to SharePoint Online
Connect-SPOService -url $AdminSiteURL -credential $Credential

#delete sharepoint online site collection using powershell
Remove-SPOSite -Identity $SiteCollURL -NoWait -Confirm:$false

Write-Host "Site collection has been deleted!"

SharePoint Online: Remove Site Collection with PowerShell
You can also use SharePoint Online Management Shell to delete site collection:
#Variables
$AdminSiteURL = "https://crescent-admin.sharepoint.com/"
$SiteCollURL = "https://crescent.sharepoint.com/sites/itgroup"

#Connect to sharepoint online
Connect-SPOService -url "https://crescent-admin.sharepoint.com/" -credential (Get-Credential)

#sharepoint online powershell delete site collection
Remove-SPOSite -Identity $SiteCollURL -NoWait -Confirm:$false
This PowerShell deletes site collection in SharePoint online. Please note, The Remove-SPOSite does not delete a site collection permanently. But it moves the site collection to the Recycle Bin.

Remove Site Collection using PowerShell CSOM
Here is how to delete a site with client side object model PowerShell script:
#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"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"

#delete site collection sharepoint online powershell
Function Remove-SPOSiteCollection($AdminCenterURL,$SiteURL)
{
    Try {
        #Setup Credentials to connect
        $Cred= Get-Credential
             
        #Setup the Context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminCenterURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
        #Get the tenant object 
        $Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($Ctx)

        Write-Host -f Yellow "Deleting site collection..." 
        #delete a sharepoint online site powershell
        $Tenant.RemoveSite($SiteURL) | Out-null
        #$Tenant.RemoveDeletedSite($SiteURL) #To delete site from recycle bin!
        $ctx.ExecuteQuery()
        Write-host "Site Collection Deleted Successfully!" -foregroundcolor Green
    }
    catch {
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
}
#Set Parameters
$AdminCenterURL = "https://Crescenttech-admin.sharepoint.com"
$SiteURL = "https://crescenttech.sharepoint.com/sites/procurement"

#Call the function to delete site collection
Remove-SPOSiteCollection $AdminCenterURL $SiteURL
To Delete a SharePoint Online Site Collection, You need to be a Site Collection Administrator / SharePoint Online Admin / Global Admin!

Permanently Delete the SharePoint Online Site Collection:
When you delete a site collection, it goes to the recycle bin. You may want to permanently delete that site collection to reuse the storage space or recreate a new site collection with the same URL. Because, If you try to create a site collection with a deleted site in the recycle bin, SharePoint gives you a message "The site address is available with modification" and doesn't allows you to use the same URL!
powershell to delete sharepoint online site collection

How to Permanently Delete a Site Collection in SharePoint Online?
To delete a site collection permanently in SharePoint Online, Login to the new admin center site >> Click on "Deleted Sites" from left navigation. This lists all deleted sites in the tenant within the past 93 days. Select the site to delete and click on the "Permanently Delete" button in the toolbar and confirm the prompt.
sharepoint online permanently delete site collection powershell
However, if you try to delete a modern Office 365 group site permanently using above steps, You'll find the Permanently delete button is disabled! So the only way to permanently remove deleted  Modern sites is: PowerShell!

SharePoint Online PowerShell to Delete Site Collection Permanently from Recycle bin
To permanently delete site collection from recycle bin, use the below script in SharePoint Online Management Shell: 
$DeletedSiteCollURL = "https://crescent.sharepoint.com/sites/deletedsite"
#Remove Deleted SharePoint Online Site Collection permanently
Remove-SPODeletedSite –identity $DeletedSiteCollURL -Confirm:$false
This deletes the site from the recycle bin.

SharePoint Online PnP PowerShell to Delete Site Collection
Here is how to delete a site collection in SharePoint Online using PnP PowerShell
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/Procurement"

#Get Credentials to connect
$Cred = Get-Credential

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
#sharepoint online delete site collection powershell
Remove-PnPTenantSite -Url $SiteURL -Force 
To permanently delete site collection with PnP PowerShell, use -SkipRecycleBin switch.
Remove-PnPTenantSite -Url $SiteURL -Force -SkipRecycleBin
To remove a deleted site collection from recycle bin with PnP PowerShell, use:
Remove-PnPTenantSite -Url $SiteURL -FromRecycleBin 

2 comments:

  1. Do I need Tenant admin roles to delete a site collection?

    ReplyDelete
    Replies
    1. Site collection Administrators can delete a site from site settings page or with PnP PowerShell. However for SharePoint Online Admin Center or SPO cmdlets, You need to have "SharePoint Online Administrator" permission.

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.