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 and libraries, sites to 93 days. Deleted items in recycle bin occupy storage and should be cleared to regain the space. 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.
    sharepoint online empty recycle bin powershell
  • 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 Site collection Admin rights, you can use 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

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 recycle bin 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 first stage or 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 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

#Config Variables
$SiteURL = "https://crescenttech.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 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 second stage recycle bin, use -SecondStageOnly switch. You can also use: Move-PnpRecycleBinItem cmdlet to clear first stage recyle 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

  • October 12, 2021 at 6:02 PM

    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
  • February 27, 2021 at 1:57 AM

    Thanks Nico! You’re a life saver

    Reply
  • January 5, 2021 at 1:45 PM

    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
  • April 1, 2020 at 5:44 PM

    Same here, -rowlimit not working

    Reply
  • November 4, 2019 at 4:18 PM

    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
    • March 13, 2020 at 12:02 PM

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

      Reply
    • March 23, 2020 at 1:16 PM

      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
    • May 27, 2020 at 4:00 PM

      Same here even set with 1…

      Reply
    • July 7, 2020 at 6:51 AM

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

      Reply
    • August 4, 2020 at 1:34 AM

      Did anyone have an answer to this?

      Reply
    • August 4, 2020 at 11:20 AM

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

      Reply
  • September 6, 2019 at 3:27 PM

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

    Reply
  • August 1, 2018 at 10:04 PM

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

    Reply
    • March 19, 2019 at 3:16 PM

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

      Reply

Leave a Reply