SharePoint Online: Empty Recycle Bin using PowerShell

Here is a PowerShell script to empty the recycle bin for all SharePoint Online sites that are in a CSV file using the PNP module. I created this by tweaking the script found here: SharePoint Online: Empty Recycle Bin using PowerShell - SharePoint Diary

Empty the Recycle Bin for SharePoint Online Sites
Author: Tom DeMeulenaere  - Jan 4, 2023
- This Script uses the SharePoint online PNP module to connect to each sharepoint url in a CSV and cycles through to empty the 1st and 2nd stage recycle bins
$SPAdminURL = ""
$siteInfo = Import-CSV C:\SWSetup\SharepointSites.csv
$AcctName =
Connect-PnPOnline -Url $SPAdminURL -Interactive
Function Empty-RecycleBin($url){
    #Connect to Tenant Admin Site
    Connect-PnPOnline -Url $s.URL -Interactive
    Write-host "Started Recycle Bin Empty for "$s.URL -f Green
    #Empty Reycle Bins: Both 1st stage and 2nd Stage (For Recycle Bins with less than 5000 items)
    try{Clear-PnPRecycleBinItem -All -Force}
    catch [Microsoft.SharePoint.Client.ServerException]{
            #Get recycle bin items in batches and delete them permanently (For Recycle Bins that have 5000+ items)
            While( (Get-PnPRecycleBinItem -RowLimit 500) -ne $null){Get-PnPRecycleBinItem -RowLimit 500 | Clear-PnPRecycleBinItem -Force}
    catch [Microsoft.SharePoint.Client.ClientRequestException]{
         Set-SPOUser -Site $s.URL -IsSiteCollectionAdmin $true -LoginName $AcctName
 foreach ($s in $siteInfo)
    $start = Get-Date
    Write-host "Completed Recycle Bin Empty for "$s.URL" Time Elapsed: "(New-TimeSpan -Start $start -End (Get-Date)) -f Yellow


Share on Google Plus

About Tom DeMeulenaere

Highly accomplished information technology professional with extensive knowledge in System Center Configuration Manager, Windows Server, SharePoint, and Office 365.
    Blogger Comment


Post a Comment

Note: Only a member of this blog may post a comment.