SharePoint Online: Empty Recycle Bin using PowerShell

Requirement: Empty recycle bin in SharePoint Online using PowerShell.

How to Empty the Recycle Bin in SharePoint Online?

The Recycle bin capability in SharePoint Online is used to retain deleted items such as list items, documents, lists, and document libraries, similar to the Windows recycle bin on your desktop PC. Deleted items in the recycle bin occupy storage for a 93-day retention period and consume the site collection quota. So, we should clear the recycle bin to regain the space immediately. As a SharePoint Online administrator, you can empty the Recycle Bin to free up storage space. You can empty the Recycle Bin for your entire SharePoint Online environment, or for individual SharePoint site collections. This can be done either through the web browser or by using PowerShell.

To clear the SharePoint Online recycle bin at the site level, follow these steps:

  1. Navigate to your SharePoint Online site, and Click on Site Settings Gear >> Choose “Site Contents”.
  2. From the “Site contents” page, click on the “Recycle Bin” link in the top-right corner. (In modern Team sites and communication sites, You can click on “Recycle Bin” from the navigation menu bar to access the recycle bin.)empty sharepoint online recycle bin using powershell
  3. You can now click the “Empty recycle bin” button in the toolbar! Or, you can select all items check box and click on the “Delete Selection” link to delete items from recycle bin in SharePoint Online. This moves items to the second stage recycle bin.
  4. Empty site collection recycle bin SharePoint Online: If you have the site collection Admin rights, you can use the Site collection Recycle bin page at Settings >> Site information >> View All Site Settings >> Recycle bin (Under Site collection Administration) to access deleted items of all users and all sites in the site collection (URL shortcut: /_layouts/15/AdminRecycleBin.aspx?view=5).
  5. Empty second stage recycle bin in SharePoint Online: Once the first-stage recycle bin is cleared, You can click on the “Second-stage recycle bin” link to access the second stage recycle bin (Open the first stage recycle bin and then use this shortcut for the second stage recycle bin: /_layouts/15/AdminRecycleBin.aspx?view=5#view=13) and empty that as well.
    sharepoint online recycle bin powershell

BTW, You should have at least edit permissions to access and restore deleted content from the recycle bin, and Deleted items from Second-Stage or Site Collection Recycle Bins are permanently removed. 

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="Salaudeen@crescent.com"

#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

How do I delete files from the second stage Recycle Bin? Well, To empty the 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 bins, use this PowerShell script:

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

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#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 Site collection
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 another post for SharePoint On-Premises: PowerShell to Empty Recycle bin in SharePoint

Clear Old Items from the SharePoint Recycle Bin

The SharePoint Recycle bin retains deleted items for 93 days. What if you want to clear all old items, say, deleted 30+ days ago?

#Parameters
$SiteURL =  "https://crescent.sharepoint.com/sites/Retail"
$Threshold = 30 #Number of days

#Calculate Deleted Date threshold
$DeletedDateThreshold = (Get-Date).AddDays(-$Threshold)

#Connect to the Site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get recycle bin items deleted more than specific number of days
$DeletedItems = Get-PnPRecycleBinItem -RowLimit 500000 | Where {$_.DeletedDate -le $DeletedDateThreshold}

#Remove deleted items permanently
$DeletedItems | Clear-PnPRecycleBinItem -Force

Similarly, To clear the tenant recycle bin and permanently remove deleted sites, use: How to Empty Tenant Recycle Bin in SharePoint Online?

How do I recover a deleted file in SharePoint Online?

To recover deleted files in SharePoint Online: Log in to SharePoint site >> Click on Settings gear, choose “Site contents” >> Click on the “Recycle Bin” button in the top-right corner. Select the files you’d like to recover and click the “Restore” button. This restores the items to their original location. If you don’t find your deleted items, try the second-stage recycle bin. You can also use PowerShell to recover files in SharePoint Online.
More info: Recover deleted files in SharePoint Online

How do I delete all items from a SharePoint Online list in PowerShell?

Browse to your list SharePoint list, and select all items in the list by ticking the checkbox beside the first column on the list. Click the “Delete” button and confirm the prompt. The selected item will be sent to the recycle bin. SharePoint allows you to select 100 items at once! However, You can select all items by scrolling to the bottom of the page. You can also use PowerShell to remove all list items in SharePoint Online.
More info: SharePoint Online delete all items from List

How do I delete a SharePoint Online Site collection?

You can delete a SharePoint Online site from the SharePoint Admin Center, Site settings of the site, or using PowerShell. To delete from the SharePoint Online Site collection, follow these steps: Log in to SharePoint as the site collection administrator >> Click on the Settings gear >> Site Information >> Click on the “Delete Site” link!
More info: Delete a Site Collection in SharePoint Online

How do I access the 2nd stage Recycle Bin in SharePoint online?

To access the second-stage Recycle Bin in SharePoint Online, Login to the SharePoint site as site collection Admin >> Click on the Settings gear icon >> Choose “Site Contents”. On the Site Contents page, click the Recycle Bin link, located in the top-right corner. Scroll down to the bottom and click on the “Second-stage” Recycle bin link. URL shortcut from the first stage recycle bin to the second stage: /_layouts/15/AdminRecycleBin.aspx?view=5#view=13

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

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

  • I love your powershell scripts, but I am new to this and need basic instructions on how to create the script from scratch and then how to add it to the sharepoint site to run automatically. Sorry for the newbee question.

    Reply
  • Also possible using SharePoint REST
    POST /sites//_api/site/getrecyclebinitems/MoveAllToSecondStage

    POST /sites//_api/site/getrecyclebinitems/DeleteAllSecondStageItems

    Reply
  • Saludeen – in our office, your name has become synonymous with SharePoint wizardry – can you help us with this one?

    Like Jorge, we are also looking for a way to delete older items from the recycle bin(s). We basically want to change the default from 93 days in recycle bin, to 30 days. Can we fetch the Date Deleted property and use that to filter?

    Reply
    • Sure! Post has been updated with the script to clear old items from the recycle bin.

      Reply
  • Is there a way to delete by date from recycled bin firts or Second?

    Reply
  • this works ( use at own risk and test first 🙂 )
    change the 500 to the amount of times you want to run it .
    I removed around 300k files in 1 hour
    1..500 | foreach {
    #Config Variables
    $SiteURL = “https://crescent.sharepoint.com/sites/marketing”

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

    Clear-PnPRecycleBinItem -All -RowLimit 1000 -force
    }

    Reply
    • Tommy, Thanks for your assistance, but I need to delete by delete date from first or second stage, in your script only delete by items number.

      Reply
  • 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

Your email address will not be published. Required fields are marked *