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, then 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. Within this time frame, you may have to remove them from the recycle bin to free up storage space.

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

  • Log in to your OneDrive site, click on the “Recycle bin” link from the left navigation.
  • Now you can see all the files and folders that were deleted in a descending list, based on the date when they were removed.
  • 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
  • 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
  • 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”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 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 -UseWebLogin

#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 bin’s 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
    
    Disconnect-PnPOnline
}

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!

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

  • March 22, 2021 at 10:49 PM

    Very helpful, thanks!

    Reply
  • March 2, 2021 at 3:11 PM

    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