Completely Delete SharePoint Online Sites using PowerShell


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:

  1. Remove-SPOSite
  2. 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
    }
}

Using PowerShell to manage SharePoint Online


Anybody who knows me or has ever read a single post of mine knows I’m a big PowerShell geek. I like it, I love it and I always want some more of it. Bad song lyrics aside, I quite literally use PowerShell every day for one task or another. Over the past 6 months or so I’ve been quietly working on creating some Cmdlets/Functions that will allow me to run PowerShell against Office 365 SharePoint Online. This might sound easy – but spoiler alert – it’s not. That’s not to say it’s difficult, if you’re a developer who is comfortable with CSOM, you’ll be just fine. However, I didn’t have those skills when I took off on this adventure; so needless to say it’s been a bit of a learning experience, combined with a lot of trial & error.

There are really a few key steps which must be taken prior to getting SharePoint Online and PowerShell to talk to one another.

  1. Set up the SharePoint Online Management Shell Windows PowerShell environment
  2. Run Connect-SPOService to connect to a SharePoint Online Administration Center

Once you’ve completed those two simple tasks, you’ve got the baseline environmental requirements in place to start using PowerShell with SharePoint Online. However, if I was writing a blogpost to tell you how to run the 30 built-in cmdlets you get by loading the Microsoft.Online.SharePoint.PowerShell module; well, you’d be left wanting more.

To really start doing the fun developer stuff, you will want to make sure you load the Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime assemblies into your session. I’ve written a nice little function for this, which looks like:

function Add-SPOAssemblies {
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null
}

Then, to run it – just use:

Add-SPOAssemblies

Pretty simple, and it just loads those two assemblies so you can access all the goodies that come with them.

Once you’ve gotten there, you can start to do things like create ClientContext against an SPWeb:

$webUrl = "SPOWebUrl"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
$creds = Get-Credential -Message "Please enter your Office 365 Administrative credentials"
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($creds.UserName,$creds.Password)
$spoweb = $ctx.Web
$ctx.Load($spoweb)
$ctx.ExecuteQuery()

So that’s cool, but it hasn’t done anything yet! Well try this:

$spoweb.Title

Ohhhh… So now that’s cool! Once you’ve gotten this far, i’m sure you can start to see the possibilities.

In case you want to wrap that up into a nice function, here’s a simple example that I think works well:

function New-SPOServiceContext {
[CmdletBinding()]
Param(
[System.String]$EmailAddress,
[System.String]$SPOWebUrl
)
try {
$assemblies=Add-SPOAssemblies
}
catch {
Write-Error "Unable to load Microsoft.SharePoint.Client assemblies"
}
if($assemblies){
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SPOWebUrl)
$creds = Get-Credential -Message "Please enter your Office 365 Administrative credentials" -UserName $EmailAddress
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($creds.UserName,$creds.Password)
$spoweb = $ctx.Web
$ctx.Load($spoweb)
$ctx.ExecuteQuery()
}
}

And of course to run it, use:

New-SPOServiceContext -EmailAddress your.email@your-company.com -SPOWebUrl https://something.sharepoint.com

“But wait, at the beginning you said this was challenging – this was way too easy!?”

Well, I wasn’t used to having to create a context object, then a credential object, followed by a web variable, and then I have to load and execute a query?! All new to me, but again – I haven’t done very much (read: any) development on the client side.

I could share loads of other cool things I’ve done, but you’ll have to wait while I clean up and finish some of the functions I’ve been working on. Until then, happy PowerShelling!