Skip to content

Instantly share code, notes, and snippets.

@reshmee011
Created August 2, 2024 09:30
Show Gist options
  • Save reshmee011/5864ab7e4a6a453f2e343d06d72ed70c to your computer and use it in GitHub Desktop.
Save reshmee011/5864ab7e4a6a453f2e343d06d72ed70c to your computer and use it in GitHub Desktop.
delete_m365_ConnectedTeamSite
Try {
$AdminCenterURL="https://contoso-admin.sharepoint.com/"
$SiteURL = "https://contoso.sharepoint.com/sites/test"
#Connect to SharePoint admin site
Connect-PnPOnline -Url $AdminCenterURL -Interactive
#Get the SharePoint Online site
$Site = Get-PnPTenantSite -Identity $SiteURL -errorAction Ignore
#Delete the Office 365 Group
Remove-PnPMicrosoft365Group -Identity $Site.GroupId -errorAction Ignore
#permanently remove the m365 group
Remove-PnPDeletedMicrosoft365Group -Identity $Site.GroupId -errorAction Ignore
#Permanently Delete the SharePoint site
$dleted = $false;
do {
Start-Sleep -Seconds 60
try {
Remove-PnPTenantSite -Url $SiteUrl -Force -SkipRecycleBin
$dleted = $true;
}
catch {
Write-Host "Delete site"
}
try {
Remove-PnPTenantDeletedSite -Url $SiteUrl -Force -SkipRecycleBin
$dleted = $true;
}
catch {
Write-Host "Permanently delete site"
}
} while (!$dleted)#>
}
Catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment