SharePoint Online: Empty Recycle Bin using PowerShell

Requirement: Empty recycle bin in SharePoint Online using PowerShell

How to Empty Recycle bin in SharePoint Online?

Recycle bin capability in SharePoint Online is used to retain deleted items such as list items, documents, lists, libraries, and sites for 93 days. Deleted items in the recycle bin occupy storage and should be cleared to regain the space immediately. To clear SharePoint Online recycle bin at the site level, follow these steps:

  • Navigate to your SharePoint Online site, Click on Site Settings Gear >> Choose “Site Contents”
  • From the “Site contents” page, click on “Recycle Bin” link in the top-right corner. You can also access the Recycle bin from quick launch navigation (if it’s available!)
    sharepoint online recycle bin powershell
  • You can click on “Empty recycle bin” button in the toolbar now! Or you can select all items and click on “Delete Selection” link to delete items from recycle bin in SharePoint Online. This moves items to second stage recycle bin.
  • If you have the site collection Admin rights, you can use the Recycle bin link at Site Settings >> Recycle bin (Under Site collection Administration) to access deleted items of all users and all sites in the site collection.
    sharepoint online recycle bin powershell

SharePoint Online: Empty Recycle bin using PowerShell

So, we have emptied the SharePoint Online recycle bin manually. But what if there was a way to automate it? PowerShell to the rescue! I’ll show you how to use PowerShell to empty your SharePoint Online recycle bin. Here is the PowerShell to empty recycle bin in SharePoint Online:

#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
$UserName="[email protected]"

#Get the password to connect 
$Password = Read-host -assecurestring "Enter Password for $UserName"
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$Password)
 
Try {    
    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $Credentials
    
    #Get the recycle bin
    $Site = $Context.Site
    $RecycleBinItems = $Site.RecycleBin
    $Context.Load($Site)
    $Context.Load($RecycleBinItems)
    $Context.ExecuteQuery()

    Write-Host "Total Number of Items found Recycle Bin:" $RecycleBinItems.Count
    #sharepoint online powershell empty recycle bin
    $RecycleBinItems.DeleteAll()
    $Context.ExecuteQuery()
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

This permanently deletes all items from both first-stage, and second-stage recycle bins of the site collection.

SharePoint Online: Empty First Stage Recycle Bin

At times, You may have to delete items from either of the recycle bin. Use these scripts if you want to empty the first stage or the second stage recycle bins:

PowerShell to Delete from recycle bin

Try {    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = $Credentials
    
    #Move All Deleted Items to 2nd Stage Recycle bin
    $Ctx.Site.RecycleBin.MoveAllToSecondStage()
    $Ctx.ExecuteQuery()
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

PowerShell to Empty Second Stage Recycle bin in SharePoint Online:

To empty site collection’s second stage recycle bin, use this PowerShell script

Try {    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = $Credentials
    
    #Delete All Items from 2nd Stage Recycle bin
    $Ctx.Site.RecycleBin.DeleteAllSecondStageItems()
    $Ctx.ExecuteQuery()
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

This empties the site collection recycle bin in SharePoint Online.

SharePoint Online: PowerShell to Delete Site from Recycle Bin

To delete a site collection from recycle bin permanently, use:

$Cred = Get-credential
Connect-SPOService -url "https://crescent-admin.sharepoint.com/" -credential $Cred
Get-SPODeletedSite | Select URL 

To Remove Deleted SharePoint Online Site Collection permanently:

Remove-SPODeletedSite -identity $DeletedSiteCollURL -Confirm:$False

SharePoint Online: Empty Recycle bin using PnP PowerShell

To clear both the first stage and second stage recycle bin’s, use this PowerShell script:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Empty Reycle Bins: Both 1st stage and 2nd Stage
Clear-PnPRecycleBinItem -All -Force

How about clearing recycle bin items with 5000+ deleted items? Well, to avoid the threshold exceeded issue, we’ll have to empty deleted items in batches. Here is how:

#Config Variables
$SiteURL =  "https://crescent.sharepoint.com/sites/marketing"

#Connect to Tenant Admin Site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get recycle bin items in batches and delete them permanently
While( (Get-PnPRecycleBinItem -RowLimit 500) -ne $null)
{
    Get-PnPRecycleBinItem -RowLimit 500 | Clear-PnPRecycleBinItem -Force
}

To empty the second-stage recycle bin, use -SecondStageOnly switch. You can also use: Move-PnpRecycleBinItem cmdlet to clear the first stage recycle bin. Here is my another post for SharePoint On-Premises: PowerShell to Empty Recycle bin in SharePoint

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

15 thoughts on “SharePoint Online: Empty Recycle Bin using PowerShell

  • Use the below instead –

    #Get recycle bin items in batches and delete them permanently
    While( (Get-PnPRecycleBinItem -SecondStage -RowLimit 500) -ne $null)
    {
    Clear-PnPRecycleBinItem -SecondStageOnly -RowLimit 500 -Force
    }

    Reply
  • Thanks Nico! You’re a life saver

    Reply
  • the cmdlets changed, the command should now be:
    #Get recycle bin items in batches and delete them permanently
    While( (Get-PnPRecycleBinItem -RowLimit 500) -ne $null)
    {
    Clear-PnPRecycleBinItem -RowLimit 500 -All -Force
    }

    Reply
  • Same here, -rowlimit not working

    Reply
  • I am unable to run the script due to the following exception:
    The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.

    Reply
    • When you have more than 5000 deleted items, use: -RowLimit parameter as in the PnP PowerShell example in this article.

      Reply
    • I tried using -rowlimit switch, still got the error “The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.”
      Even with a row limit set to 50…

      Reply
    • Same here even set with 1…

      Reply
    • I have the same issue :(, could somebody find the fix?

      Reply
    • Did anyone have an answer to this?

      Reply
    • Be sure you have the latest PnP PowerShell module! You can update your existing module with:
      Update-Module SharePointPnPPowerShellOnline

      Reply
  • Very helpful. I used the first script and it worked perfectly! Thanks Salaudeen.

    Reply
  • Is there a cmdlet for emptying second-stage recycle bin?

    Reply
    • Use: $Ctx.Site.RecycleBin.DeleteAllSecondStageItems() or Clear-PnPRecycleBinItem -SecondStageOnly

      Reply

Leave a Reply