The OneDrive Recycle Bin stores deleted files for a specific retention period before permanent deletion. Using PnP PowerShell, administrators can automate the management of the OneDrive Recycle Bin, including:
Viewing deleted items
Restoring items
Permanently deleting items
Why use PnP PowerShell?
- Faster bulk operations across multiple users
- Automates cleanup to optimize storage
- Restores critical files without user intervention
Prerequisites
Before executing PowerShell commands, ensure the following:
1️⃣ Install PnP PowerShell (if not installed)
Install-Module -Name PnP.PowerShell -Force -AllowClobber
2️⃣ Connect to SharePoint Online (Admin Center)
Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com" -Interactive
Use Global Administrator or SharePoint Admin credentials.
Step 1: Retrieve OneDrive Recycle Bin Items
To list all deleted items in a user’s OneDrive Recycle Bin:
Get-PnPRecycleBinItem -FirstStage | Select Title, ItemType, DeletedBy, DeletedDate
Retrieves deleted files from the First-Stage Recycle Bin.
ItemType can be File or Folder.
For the Second-Stage Recycle Bin (items deleted from First-Stage):
Get-PnPRecycleBinItem -SecondStage | Select Title, ItemType, DeletedBy, DeletedDate
First-Stage: Deleted by user (restorable)
Second-Stage: Deleted from First-Stage (harder to recover)
Step 2: Restore Deleted Items from the Recycle Bin
To restore all items from the First-Stage Recycle Bin:
Get-PnPRecycleBinItem -FirstStage | Restore-PnPRecycleBinItem
Restores all deleted files and folders to their original locations.
Restore a specific file:
powershellCopyEdit$deletedFile = Get-PnPRecycleBinItem -FirstStage | Where-Object { $_.Title -eq "important.docx" }
Restore-PnPRecycleBinItem -Identity $deletedFile.Id
Restores only “important.docx”.
Restore all files deleted by a specific user:
$deletedFiles = Get-PnPRecycleBinItem -FirstStage | Where-Object { $_.DeletedBy -eq "user@yourtenant.com" }
$deletedFiles | ForEach-Object { Restore-PnPRecycleBinItem -Identity $_.Id }
Restores all files deleted by a specific user.
Step 3: Permanently Delete Items from the Recycle Bin
To permanently delete all items from the First-Stage Recycle Bin:
Get-PnPRecycleBinItem -FirstStage | Remove-PnPRecycleBinItem -Force
Bypasses the Second-Stage Recycle Bin and deletes items permanently.
Permanently delete a specific file:
$fileToDelete = Get-PnPRecycleBinItem -FirstStage | Where-Object { $_.Title -eq "old_data.xlsx" }
Remove-PnPRecycleBinItem -Identity $fileToDelete.Id -Force
Deletes “old_data.xlsx” permanently.
Clear the Second-Stage Recycle Bin (Complete Cleanup):
Get-PnPRecycleBinItem -SecondStage | Remove-PnPRecycleBinItem -Force
Removes all files that users cannot recover.
Step 4: Automate Recycle Bin Cleanup with a Scheduled Task
To automatically clear old files from the Recycle Bin every week, save this script as Clear-OneDriveRecycleBin.ps1
:
# Connect to SharePoint
Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com" -Credentials (Get-Credential)
# Clear First-Stage Recycle Bin
Get-PnPRecycleBinItem -FirstStage | Remove-PnPRecycleBinItem -Force
# Clear Second-Stage Recycle Bin
Get-PnPRecycleBinItem -SecondStage | Remove-PnPRecycleBinItem -Force
Write-Host "OneDrive Recycle Bin cleaned successfully!"
Then, schedule the script using Windows Task Scheduler:
Runs weekly/monthly to remove unnecessary files.
Reduces storage costs by clearing old files.
Step 5: Troubleshooting Issues
1️⃣ “Access Denied” Error
Ensure you have SharePoint Administrator or Global Administrator permissions.
Run PowerShell as Administrator.
2️⃣ Deleted Files Not Appearing?
Check if files are in the Second-Stage Recycle Bin instead of First-Stage.
3️⃣ PowerShell Module Not Found?
Update PnP PowerShell:
Update-Module PnP.PowerShell