How to Delete a SharePoint Online Site Using PowerShell?

While working on a PowerShell, I needed to delete unnecessary SharePoint sites in the SharePoint admin center to free up storage space.

In this tutorial, I will show you how to delete a SharePoint Online site using PowerShell with examples.

How to Delete a SharePoint Online Site Using PowerShell

Using two different approaches, let’s check how to delete a SharePoint site through PowerShell. Such as:

  • Delete a SharePoint site using SharePoint Online Management PowerShell
  • Delete a Sharepoint site using PnP PowerShell

Additionally, we will discuss how to delete multiple SharePoint sites using SharePoint management as well as PnP PowerShell.

NOTE:

To delete a SharePoint site, the user needs to have admin center access as a SharePoint Administrator or Global Administrator and be a Site owner or member with full control over the SharePoint site.
If you are a SharePoint Administrator and not a site member or owner, it will not allow you to delete the SharePoint site.

Delete a SharePoint Site Using SharePoint Online Management PowerShell

This section will show you how to delete a SharePoint site through SharePoint Online Management PowerShell.

There are two methods to delete a SharePoint site:

  1. Remove-SPOSite
  2. Remove-SPODeletedSite

You need to use either Windows PowerShell ISE or Visual Studio Code to execute PowerShell commands.

Delete a SharePoint Site Using PowerShell Remove-SPOSite

The Remove-SPOSite cmdlet is used to delete a particular SharePoint site from the SharePoint Admin center and move it to the SharePoint Deleted site/recycle bin page.

Syntax:

Remove-SPOSite
-Identity <SpoSitePipeBind>
[-Confirm]
[-Nowait]
[-WhatIf]

Where,

  • Identity: To specify the exact SharePoint site URL.
  • Confirm: It will ask for your confirmation before executing the command.
  • Nowait: it specifies that the script will be expected immediately without any delay.
  • WhatIf: Specifies what would happen if the command runs. The command is not run.

Example-1:

This example removes a SharePoint site from the SharePoint admin center and moves it to the recycle Bin.

Remove-SPOSite -Identity https://szg52.sharepoint.com/sites/ProjectSite
  • Executing the above script by clicking the Run button will prompt a confirmation message. Click Yes to All, Yes [if you want to delete the SharePoint site], or No. The specific site will be deleted.
delete a SharePoint site using PowerShell
  • To check the deleted SharePoint site, go to the SharePoint Admin center [https://<tenant>-admin.sharepoint.com] -> click on the Deleted sites under the Sites.
Delete a SharePoint Online site using PowerShell
  • You can find the SharePoint deleted site on the Deleted sites page.
delete SharePoint web application using PowerShell

Example-2:

This example will remove a SharePoint site with a confirmation message and move to the deleted sites.

Remove-SPOSite -Identity https://szg52.sharepoint.com/sites/AdminTeam -Confirm
  • Click the Run button, and a confirmation message will appear -> Click Yes or No. The SharePoint site will be deleted.
how to deleted SharePoint site using PowerShell

NOTE: If you use the -Confirm:$false parameter, the SharePoint site will be automatically deleted from the Active sites and moved to the Deleted sites.

Example-3:

This example will delete a SharePoint site immediately from the SharePoint admin center and put it in the recycle bin.

Remove-SPOSite -Identity https://szg52.sharepoint.com/sites/DatabaseTeam -NoWait
  • Click on the Run button, and it will ask for a confirm message to appear -> Click Yes or No.
permanently delete SharePoint site using PowerShell

Example-4:

This example will run what would happen if the command were executed without making any changes and displaying a list of affected objects in the console window.

Remove-SPOSite -Identity https://szg52.sharepoint.com/sites/DeereandCompany -WhatIf
  • Click on the Run button, and it gives Performing the operation “Remove-SPOSite” on target “https://szg52.sharepoint.com/sites/DeereandCompany” in the console.
delete SharePoint site with PowerShell

NOTE: When the WhatIf [Parameter] is used, the SharePoint site is not deleted from the SharePoint admin center. Instead, it shows the target URL.

Delete a SharePoint Site Using Remove-SPODeletedSite PowerShell

The Remove-SPODeletedSite cmdlet permanently removes a SharePoint site from the recycle bin or deleted sites.

Syntax:

RemoveSPODEletedSite
-Identity <SpositePipeBind>
[-Confirm]
[-NoWait]
[-WhatIf]

Example:

This example will permanently delete the SharePoint site from the SharePoint deleted sites page without any delay.

Remove-SPODeletedSite -Identity https://szg52.sharepoint.com/sites/ProjectSite -NoWait
  • Click on the Run button, and it will ask for a confirm message to appear -> Click Yes or No. The specific SharePoint site was deleted from the recycle bin.
how to delete SharePoint online site using PowerShell

This is how to delete a SharePoint site from SharePoint Online Management PowerShell.

Delete a SharePoint Group Connected Site Using SharePoint Online Management PowerShell

This section will show you how to delete a SharePoint group-connected site.

The Remove-SPOSite command deletes a SharePoint group-connected site, and we will face an error message: “This site belongs to a Microsoft 365 group. To delete the site, you must delete the group.”

Remove-SPOSite -Identity https://szg52.sharepoint.com/sites/hiringTeam
PowerShell delete a SharePoint site

To overcome this error, you use the Remove-SPODeletedSite command.

Remove-SPODeletedSite -Identity  https://szg52.sharepoint.com/sites/hiringTeam
  • Click on the Run button and automatically delete the SharePoint group-connected site.
PowerShell delete a SharePoint 2013 site

NOTE: It will delete the SharePoint site, but it will not delete the Group.

This is how to delete a SharePoint group-connected site using SharePoint Online Management PowerShell.

Delete a SharePoint Site from PnP PowerShell

In this section, I will show you how to delete a SharePoint site through PnP PowerShell. We use the Remove-PnPTenantSite command to do this.

I have to use the Visual Studio code editor to execute the PnP PowerShell command.

Delete a SharePoint Site Using Remove-PnPTenantSite PowerShell

The Remove-PnPTenantSite is used to delete a SharePoint site listed in your  SharePoint admin center and move to the SharePoint deleted site or recycle bin.

Syntax:

Remove-PnPTenantSite
[-Url] <String>
[-FromRecycleBin]
[-SkipRecycleBin]

where,

  • Url: It Specifies the SharePoint site URL.
  • FromRecycleBin: It will specify the deleted SharePoint site from the recycle bin.
  • SkipRecycleBin: The SharePoint site has been deleted permanently, and it will skip the recycle bin.

Example-1:

This will remove the SharePoint site with the Url and move it to the recycle bin.

Remove-PnPTenantSite -Url "https://szg52.sharepoint.com/sites/ResourceDevelopment"
  • Click on the Run button, and it will ask for a confirm message -> Write Y [ If you want to delete a SharePoint site] or N. The specific SharePoint site will be deleted.
delete SharePoint site using PnP PowerShell

Example-2:

This example will delete a SharePoint site with Url from the recycle bin.

Remove-PnPTenantSite -Url "https://szg52.sharepoint.com/sites/ResourceDevelopment" -FromRecycleBin
  • Click the Run button, and it will ask for confirmation ->Write Y or N. The SharePoint site was permanently deleted from the recycle bin.
delete the SharePoint site using PowerShell

Example-3:

This will delete a SharePoint site with Url and skip the recycle bin.

Remove-PnPTenantSite -Url "https://szg52.sharepoint.com/sites/AccountManagement" -SkipRecycleBin
  • Click the run button, and a confirmation message will appear -> Write Y or N.
how to delete a SharePoint site using PowerShell

This is how to delete a SharePoint site from PnP PowerShell.

Delete Multiple SharePoint Sites Using SharePoint Online Management PowerShell

In PowerShell, The Remove-SPOSite command is used to delete multiple SharePoint sites from the SharePoint admin center and move the recycle bin.

Example:

$siteUrls = @(
"https://szg52.sharepoint.com/sites/Admin",                    
"https://szg52.sharepoint.com/sites/DeereandCompany"
 )
 # Loop through the list and delete each site
 foreach ($url in $siteUrls) {
Remove-SPOSite -Identity $url -Confirm:$false
 }

Where,

  • $siteUrls: It specifies the URLs of the SharePoint sites.
  • foreach: It will loop iterates through each site URL.
  • Identity: It specifies the URL of the site.
  • Confirm: It will ask for a confirmation message for each deletion.

Click the Run button, to automatically delete multiple SharePoint sites in the SharePoint admin center to move the Deleted sites.

Delete multiple SharePoint sites using PowerShell

This is how to delete multiple SharePoint sites using SharePoint Online Management PowerShell.

Delete Multiple SharePoint Sites Using PnP PowerShell

The Remove-PnPTenantSite is used to delete multiple SharePoint sites from the SharePoint admin center and move to the SharePoint deleted site or recycle bin.

Example:

$siteUrls = @(
    "https://szg52.sharepoint.com/sites/DeereandCompany",
    "https://szg52.sharepoint.com/sites/SharePointteam"
    # Add more site URLs as needed
)
# Loop through each site URL and delete it
foreach ($siteUrl in $siteUrls) {
    Remove-PnPTenantSite -Url $siteUrl 
}
  • Click the Run button. A confirm message will appear: write Y or N according to siteurls they will ask.
pnp PowerShell delete multiple SharePoint site

This is how to delete multiple SharePoint sites using PnP PowerShell.

In this tutorial, I hope you understand how to delete a SharePoint site(s) using SharePoint Online Management PowerShell and PnP PowerShell, along with the different examples.

Moreover, you may like some more PowerShell articles: