OneDrive for Business: Empty Recycle Bin using PowerShell

Requirement: Empty recycle bin in OneDrive for Business using PowerShell.

How to Empty Recycle Bin in OneDrive for Business?

When you delete a file or folder from OneDrive for Business, it goes to the first stage, “Recycle Bin”. If you delete it from the first-stage recycle bin, the file or folder goes to the second-stage recycle bin. Deleted items in the recycle bin stay there until they’re permanently removed in 93 days automatically. You may have to remove them from the recycle bin within this time frame to free up storage space.

You can empty the “Recycle bin” in the OneDrive Site to reclaim storage space. Here’s how to empty your OneDrive’s Recycle bin:

  1. Log in to your OneDrive site, and click on the “Recycle bin” link from the left navigation.
  2. Now you can see all the files and folders that were deleted in a descending list based on the date when they were removed.
  3. To empty your Recycle bin, click the “Empty recycle bin” button on the top and confirm the prompt that tells you that it will delete all the items that are found in the Recycle bin, by pressing “Yes”.empty recycle bin onedrive for business powershell
  4. Once all files and folders are deleted from End-User recycle bin, wait for some time and click on the “Second-Stage” Recycle bin.onedrive for business empty recycle bin powershell
  5. Again, click on “Empty Recycle Bin” in the second-stage recycle bin to remove files permanently and empty your OneDrive for Business Site’s recycle bin.onedrive for business recycle bin powershell
Important: Be sure you don’t need any files or folders in the recycle bin before you deleting them permanently!

PowerShell to Empty Recycle bin in OneDrive for Business Site

Let’s empty OneDrive for Business recycle bin using PowerShell:

#Set Parameters
$OneDriveSiteURL="https://crescent-my.sharepoint.com/personal/salaudeen_crescent_com/"

#Get Credentials to connect
$Cred = Get-Credential
 
#Connect to OneDrive for Business Site
Connect-PnPOnline $OneDriveSiteURL -Credentials $Cred

#empty recycle bin onedrive for business powershell
Clear-PnPRecycleBinItem -All -force

When you have more than 5000 items in your recycle bin, both the UI method and PowerShell above may fail with the “Threshold exceeded error message. So, Here is how to clear recycle bin items in batches:

#empty first stage recycle bin in onedrive for business site
Get-PnPRecycleBinItem -FirstStage -RowLimit 5000  | Clear-PnpRecycleBinItem -Force

#PowerShell to empty 2nd Stage recycle bin in onedrive for business
Get-PnPRecycleBinItem -SecondStage -RowLimit 5000  | Clear-PnpRecycleBinItem -Force

However, You got to run the above script multiple times based on the number of items in the recycle bin. Let’s place it inside a while loop:

#Config Variables
$SiteURL =  "https://crescent-my.sharepoint.com/personal/salaudeen_crescent_com"

#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
}

OneDrive for Business: Empty Recycle Bin for All Sites using PowerShell

How about clearing recycle bins for all OneDrive sites in the Office 365 tenant?

#Set Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"

#Get Credentials to connect
$Cred = Get-Credential
 
#Connect to Tenant Admin Site
Connect-PnPOnline $AdminSiteURL -Credentials $Cred
 
#Get All OneDrive for Business Sites
$OneDriveSites = Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'"

#Loop through each site
ForEach($Site in $OneDriveSites)
{ 
    #Connect to OneDrive for Business Site
    Connect-PnPOnline $Site.URL -Credentials $Cred
    Write-Host -f Yellow "Processing Site: "$Site.URL
 
    #empty recycle bin onedrive for business powershell
    Clear-PnPRecycleBinItem -All -force    
}

A similar script works for SharePoint Online as well! How to Empty Recycle Bin in SharePoint Online using PowerShell?

In conclusion, there are two ways to empty the recycle bin in OneDrive for Business, using the web interface or the PowerShell. It’s important to note that once the recycle bin is emptied from both stages of recycle bin, the files and folders will be permanently deleted and cannot be recovered, so it is recommended to be cautious when emptying the recycle bin.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

2 thoughts on “OneDrive for Business: Empty Recycle Bin using PowerShell

  • Very helpful, thanks!

    Reply
  • Stumbled across your site while searching for exactly this type of solution, really appreciate the information. However, I do seem to be running into an issue. After iterating through the first 2000 or so OneDrive sites, I notice I appear to be getting throttled. Each subsequent Connect-PnPOnline is delayed for 5 minutes before the connection is established and then the cleanup runs quickly and without issues, but upon attempting to connect to the next site, once again, there is a 5 minute delay. Have you encountered a similar issue and can you suggest a workaround? Thank you!

    Reply

Leave a Reply

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