How to Delete and Recover a SharePoint Online Site Using PowerShell and PnP PowerShell?

If you are a SharePoint administrator, you might frequently be asked how to delete a SharePoint Online site using PowerShell or PnP PowerShell.

I will also show you how to recover a deleted SharePoint site using PnP PowerShell from the recycle bin.

Delete a SharePoint Online Site Using PowerShell

First, let me show you how to delete a SharePoint Online site using the SharePoint Online management shell. I hope you have already installed this on your system.

In PowerShell, you can use the Remove-SPOSite cmdlet to delete a SharePoint site or a site collection. This cmdlet moves the SharePoint site to the Recycle Bin. You can also recover the site from the recycle bin.

Below is the complete PowerShell script to delete a site in SharePoint.

Connect-SPOService -Url https://szg52-admin.sharepoint.com
Remove-SPOSite -Identity https://szg52.sharepoint.com/sites/marketing-NoWait

The -NoWait parameter here allows the command to run asynchronously.

Restore a Deleted SharePoint Online Site Using PowerShell

You can use the Restore-SPODeletedSite cmdlet to restore a SharePoint Online site from the Recycle Bin. Here is how you can use this cmdlet.

Connect-SPOService -Url https://szg52-admin.sharepoint.com
Restore-SPODeletedSite -Identity https://szg52.sharepoint.com/sites/marketing

Now, let me show you how to use PnP PowerShell to delete and restore a SharePoint Online site.

Delete a Site in SharePoint Online Using PnP PowerShell

If you are new to PnP PowerShell, check out an article on connecting to SharePoint Online using PnP PowerShell.

You can use the Remove-PnPTenantSite cmdlet to delete a SharePoint site collection in PnP PowerShell.

Here is the complete script:

Connect-PnPOnline -Url "https://szg52-admin.sharepoint.com" -Interactive
Remove-PnPTenantSite -Url https://szg52.sharepoint.com/sites/DemoPowerShellSite -Force

You can see the screenshot below after I executed the above PowerShell script using VS code.

Delete a Site in SharePoint Online Using PnP PowerShell

Now, if you open the recycle bin in the admin center, you can see the SharePoint site in the recycle bin.

If you want to remove the site from the recycle bin, you can run the below cmdlet.

Remove-PnPTenantDeletedSite -Identity "https://szg52.sharepoint.com/sites/DemoPowerShellSite" -Force

Restore a Deleted SharePoint Online Site Using PnP PowerShell

Now, let me show you how to restore a deleted SharePoint Online site using PnP PowerShell.

Here is the complete script.

Connect-PnPOnline -Url "https://szg52-admin.sharepoint.com" -Interactive
Restore-PnPTenantDeletedSite -Url https://szg52.sharepoint.com/sites/DemoPowerShellSite

This script will restore the above SharePoint Online site from the recycle bin.

Read Create item in SharePoint list using PnP PowerShell

This site belongs to a microsoft 365 group. To delete the site, you must delete the group

If you try to delete a SharePoint team site that is connected to a Microsoft 365 group, then when you run the Remove-PnPTenantSite cmdlet, it gives you the below error:

This site belongs to a Microsoft 365 group. To delete the site, you must delete the group.

I received the error, as you can see in the below screenshot.

This site belongs to a microsoft 365 group

To fix the error, you first need to delete the Microsoft 365 group from the admin center, and then you can run the above PnP PowerShell cmdlet.

To delete a Microsoft 365 group, open the Microsoft 365 admin center and expand “Teams & groups“. Then, delete the group from the “Active teams & groups” page.

Once the M365 group is deleted successfully, you can run the Remove-PnPTenantSite cmdlet to delete the SharePoint site.

Read Create a list in SharePoint using PowerShell

Delete SharePoint Team Site Connected to a Microsoft 365 Group using PnP PowerShell

To delete a Microsoft 365 group, you can use the Remove-PnPMicrosoft365Group PnP PowerShell cmdlet. Here is a complete script:

Connect-PnPOnline -Url "https://szg52-admin.sharepoint.com" -Interactive
$GroupEmail="Demo@szg52.onmicrosoft.com"
$Group = Get-PnPMicrosoft365Group | Where Mail -eq $GroupEmail
Remove-PnPMicrosoft365Group -Identity $Group.id
Write-host "Group Deleted Successfully!"
This site belongs to a microsoft 365 group. To delete the site, you must delete the group

Once you execute the above script, it will delete the Microsoft 365 group. And then, you can run the below script to delete the SharePoint site.

Connect-PnPOnline -Url "https://szg52-admin.sharepoint.com" -Interactive
Remove-PnPTenantSite -Url https://szg52.sharepoint.com/sites/Demo -Force

I have added a complete script like the one below to combine everything into one script. To learn more about each cmdlet, you can check out the comments I added for it.

Connect-PnPOnline -Url "https://szg52-admin.sharepoint.com" -Interactive
$siteURL="https://szg52.sharepoint.com/sites/GroupConnectedSite"
$GroupEmail="GroupConnectedSite@szg52.onmicrosoft.com"
$Group = Get-PnPMicrosoft365Group | Where Mail -eq $GroupEmail
Remove-PnPMicrosoft365Group -Identity $Group.id
Write-host "Group Deleted Successfully!"
Start-Sleep -Seconds 60

#The below cmdlet will delete the SharePoint site
Remove-PnPTenantSite -Url $siteURL -Force -SkipRecycleBin

#Then it will remove the Microsoft 365 Group
Remove-PnPDeletedMicrosoft365Group -Identity $_.GroupId

#Delete the SharePoint site from the recycle bin.
Remove-PnPTenantDeletedSite -Identity $siteURL -Force

Once you execute the above script, it will delete the SharePoint site group connected site.

Conclusion

In this tutorial, I have explained how to delete a SharePoint site using PowerShell and PnP PowerShell. I have also explained how to delete a group-connected team site in SharePoint Online using PnP PowerShell.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.