There comes a time, where manual clicking through the SharePoint navigation structure gets tedious. I found myself contemplating my life when Remote Desktoped to a slow, clunky workstation just to access SharePoint online. I was removing content types with incorrect ID's, and got that annoying "This Content Type is still in use" error. At any rate, the content type was in a recycle bin, and had to be cleared at the Web and Site level. I had to repeat this process in multiple locations, and thought "Wouldn't it be nice if I could run a script to do this for me". So I built one into my office365-devscripts library I've been working on.
You won't be able to run this directly without downloading office365-devscripts on github, but with a few modifications it would work by itself.
PowerShell - Delete-RecycleBins.ps1
#Script: Delete-RecycleBins.ps1 https://github.com/TjWheeler/office365-devscripts
#Author: Tim Wheeler (http://timwheeler.io)
#Version: 0.3
#Purpose: Recurse the web structure and clear all of the Web Recycle Bins. Then clear the Site Recycle Bin.
#notes:
param(
$env = $(Read-Host "Specify environment name"),
[ValidateSet("Dev","Test","UAT","Prod")]
[String] $environmentType = $(Read-Host "Specify EnvironmentType Dev,Test,UAT,Prod"),
[switch] $confirm = $true
)
$InformationPreference = "continue"
&("$PSScriptRoot\Start.ps1")
Check-Environment $env $environmentType
Warn-WillUpdate $env $environmentType $confirm
$scriptStartTime = Get-Date
$context = Create-Context $env -environmentType $environmentType
function RecurseWebs([Microsoft.SharePoint.Client.Web] $web)
{
$context.Load($web)
Execute-WithRetry $context
Write-Information "Emptying Recycle Bin at $($web.Url)"
$web.RecycleBin.DeleteAll()
$webs = $web.Webs
$context.Load($webs)
Execute-WithRetry $context
foreach($subWeb in $webs)
{
RecurseWebs $subWeb
}
}
try
{
$site = $context.Site
$context.Load($site)
RecurseWebs $context.Site.RootWeb
Write-Information "Emptying Site Recycle Bin at $($site.Url)"
$site.RecycleBin.DeleteAll()
$context.ExecuteQuery()
}
finally
{
Write-Information "Script started at $scriptStartTime"
Write-Information "Script finished at $(Get-Date)"
Write-Information "Time taken is $(([DateTime]::Now - $scriptStartTime).ToString())"
if($context -ne $null)
{
$context.Dispose()
$context = $null
}
}