This will be a quick and dirty post. If you’ve administered SharePoint Online at all, you know that when you delete a Site Collection it goes into a Site Collection Recycle Bin. There are times when you may want to completely remove the SPOSite immediately, including removing it from the Recycle Bin. There are two commands to accomplish this:
- Remove-SPOSite
- Remove-SPODeletedSite
That’s great, and to delete the sites you just need to run them in that order. However, I’m sort of lazy at times and want to run something ONCE and have it do the work for me. Aren’t all scripters like this?
To make this functionality possible using one call, I wrote a function which I’ll share below.
Essentially it just takes one parameter for URL, and it uses SupportsShouldProcess and ConfirmImpact to prompt you for confirmation. Assuming you oblige, it will run the Remove-SPOSite first, with Confirmation suppressed (-Confirm:$false) and with no wait. After that completes, it will use Start-Sleep and a do/while loop to wait until the SPODeletedSite status is ‘Recycled’. Once that returns true, it will run Remove-SPODeletedSite to get rid of the recycle bin item.
Here’s the Delete-SPOSite function:
Function Delete-SPOSite { [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="High")] Param( [Parameter(Mandatory)][System.String]$Url ) If($PSCmdlet.ShouldProcess($Url)) { Remove-SPOSite -Identity $Url -Confirm:$false -NoWait Write-Verbose "Waiting for site to be added to Recycle Bin" do {Start-Sleep -Seconds 3} while((Get-SPODeletedSite -Identity $Url).Status -ne 'Recycled') Remove-SPODeletedSite -Identity $Url -Confirm:$false } }