GitHub Icon Image

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.

  • CLI for Microsoft 365 using PowerShell
  • CLI for Microsoft 365 using Bash
  • PnP PowerShell
$deletedSites = m365 spo tenant recyclebinitem list -o json | 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
  m365 spo tenant recyclebinitem remove -u $deletedSite.Url --confirm
}

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

#!/bin/bash

# requires jq: https://stedolan.github.io/jq/

deletedsites=( $(m365 spo tenant recyclebinitem list -o json | jq -r '.[].Url') )

if [ ${#deletedsites[@]} = 0 ]; then
  exit 1
fi

printf '%s\n' "${deletedsites[@]}"
echo "Press Enter to start deleting (CTRL + C to exit)"
read foo

progress=0
total=${#deletedsites[@]}

for deletedsite in "${deletedsites[@]}"; do
  ((progress++))
  printf '%s / %s:%s\n' "$progress" "$total" "$deletedsite"
  m365 spo tenant recyclebinitem remove -u $deletedsite --confirm
done

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

Connect-PnPOnline -Url 'https://contoso-admin.sharepoint.com' -Interactive #Change to your tenant admin site address

$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
  Clear-PnPTenantRecycleBinItem -Url $deletedSite.Url -Wait -Force
}

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

Source Credit

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

Contributors

Author(s)
Laura Kokkarinen
Leon Armston

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