Skip to main content

Delete all (non-group connected) modern SharePoint sites

Author: Laura Kokkarinen

When you delete Microsoft 365 groups, the modern group-connected team sites get deleted with them. The script below handles the remaining modern sites: communication sites and groupless team sites.

$sparksjoy = "Cat Lovers United", "Extranet", "Hub"
$sites = m365 spo site list | ConvertFrom-Json
$sites = $sites | where { $_.template -eq "SITEPAGEPUBLISHING#0" -or $_.template -eq "STS#3" -and -not ($sparksjoy -contains $_.Title)}
if ($sites.Count -eq 0) { break }
$sites | Format-Table Title, Url, Template
Read-Host -Prompt "Press Enter to start deleting (CTRL + C to exit)"
$progress = 0
$total = $sites.Count
foreach ($site in $sites)
{
$progress++
write-host $progress / $total":" $site.Title
write-host $site.Url
m365 spo site remove --url $site.Url
}
CTRL + M