GitHub Icon Image
GitHub

Empty the tenant recycle bin

Summary

Your deleted modern SharePoint sites are not going to disappear from the UI before they have been removed from the tenant recycle bin. You can either wait for three months, delete them manually via the SharePoint admin center, or run the script below.

Warning

Please be aware this script contains a command that will remove or delete an artifact, ensure you test and understand the implications of running the script.

  • PnP PowerShell
  • CLI for Microsoft 365
  • SPO Management Shell

# SharePoint online admin center site url
$adminSiteUrl = "https://contoso-admin.sharepoint.com"

# Connect to SharePoint online admin center
Connect-PnPOnline -Url $adminSiteUrl -Interactive

# Get all deleted sites from tenant recycle bin
$deletedSites = Get-PnPTenantRecycleBinItem
$deletedSites | Format-Table Url

if ($deletedSites.Count -eq 0) 
{ 
  break 
}

Read-Host -Prompt "Press Enter to start deleting (CTRL + C to exit)"

$progress = 0
$total = $deletedSites.Count

foreach ($deletedSite in $deletedSites)
{
  $progress++
  Write-Host $progress / $total":" $deletedSite.Url

  # Permanently delete site collection from the tenant recycle bin
  Clear-PnPTenantRecycleBinItem -Url $deletedSite.Url -Wait -Force
}

# Disconnect SharePoint online connection
Disconnect-PnPOnline

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell

The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?


# Get Credentials to connect
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
   m365 login
}

# Get all deleted sites from tenant recycle bin
$deletedSites = m365 spo tenant recyclebinitem list | ConvertFrom-Json
$deletedSites | Format-Table Url

if ($deletedSites.Count -eq 0) 
{ 
  break 
}

Read-Host -Prompt "Press Enter to start deleting (CTRL + C to exit)"

$progress = 0
$total = $deletedSites.Count

foreach ($deletedSite in $deletedSites)
{
  $progress++
  Write-Host $progress / $total":" $deletedSite.Url

  # Permanently delete site collection from the tenant recycle bin
  m365 spo tenant recyclebinitem remove --siteUrl $deletedSite.Url --wait --confirm
}

# Disconnect SharePoint online connection
m365 logout

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Important changes coming to the way you login into CLI for Microsoft 365 (effective 9th September 2024) see Changes in PnP Management Shell registration in Microsoft 365


# SharePoint online admin center site url
$adminSiteUrl = "https://contoso-admin.sharepoint.com"

# Connect to SharePoint online admin center
Connect-SPOService -Url $adminSiteUrl

# Get all deleted sites from tenant recycle bin
$deletedSites = Get-SPODeletedSite
$deletedSites | Format-Table Url

if ($deletedSites.Count -eq 0) 
{ 
  break 
}

Read-Host -Prompt "Press Enter to start deleting (CTRL + C to exit)"

$progress = 0
$total = $deletedSites.Count

foreach ($deletedSite in $deletedSites)
{
  $progress++
  Write-Host $progress / $total":" $deletedSite.Url

  # Permanently delete site collection from the tenant recycle bin
  Remove-SPODeletedSite -Identity $deletedSite.Url -Confirm
}

# Disconnect SharePoint online connection
Disconnect-SPOService

Check out the SPO Management Shell to learn more at: Introduction SharePoint Online Management Shell | Microsoft Docs

Source Credit

Sample first appeared on Empty the tenant recycle bin | CLI for Microsoft 365

Contributors

Author(s)
Leon Armston
Ganesh Sanap

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI