SharePoint Online: Delete a Site Collection using PowerShell

Requirement: Delete a SharePoint Online Site Collection using PowerShell.

How to a 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. When you no longer need a SharePoint Online site (E.g. Project completed, Temporary site, etc.), you can delete it to free up storage space.

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.

If you have site collection Admin rights, You can also delete a site collection by directly going to the site >> Click on Settings >> Site Information >> Click on the “Delete Site” link!

how to delete a site in sharepoint online

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 collection >> Click on “Bulk Edit” >> Choose the “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

When you delete a SharePoint online site, all subsites, pages, lists and libraries, and content within the site are also deleted. 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
Permissions to Delete a Site in SharePoint Online
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 allow 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 the above steps, You’ll find the Permanently delete button is disabled! So the only way to permanently remove deleted Modern sites is: PowerShell! Let’s delete a site collection in SharePoint Online using PowerShell:

SharePoint Online PowerShell to Delete Site Collection Permanently from Recycle bin

We can use PowerShell to remove a deleted site in SharePoint Online. 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 

Removing stale content not only helps us to reduce storage costs, but also helps users to find the right content they’re looking for more quickly – as we remove unnecessary sites.

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

2 thoughts on “SharePoint Online: Delete a Site Collection using PowerShell

  • October 15, 2019 at 3:39 PM

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

    Reply
    • March 26, 2020 at 11:55 AM

      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.

      Reply

Leave a Reply