Does anyone know of a way to automate clearing recycle bin and second stage recycle bin for SharePoint?

I use my SharePoint strictly for scheduled backups of my OS and the items in the recycle bins are all outdated backups but I have to go back every week to clear both recycle bins because the recycle bins add to the storage quota.

  • Are you using SharePoint Online or SharePoint On-premise? Mar 28, 2023 at 5:26

1 Answer 1


You can use PowerShell script for deleting SharePoint first stage Recycle Bin and second stage Recycle Bin items.

Based on SharePoint version you are using, you can refer PowerShell scripts given below:

For SharePoint online: SharePoint Online: Empty Recycle Bin using PowerShell

For SharePoint Server / On-Premises: Empty SharePoint Recycle Bins Programmatically with PowerShell


For SharePoint Online, you can use PnP PowerShell code like below:

#Site URL
$SiteURL = "https://Crescent.sharepoint.com"
$username = "[email protected]"
$password = "XXXXXXXXX"

$encpassword = convertto-securestring -String $password -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $encpassword

#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Credentials $cred
#Empty Recycle Bins: Both 1st stage and 2nd Stage
Clear-PnPRecycleBinItem -All -Force
  • Awesome, I've gotten the SharePoint Online version to work. Is there any way to add the password in the script itself to make this entire process automated. Yes I recognize it's risky but this account itself is not important just the files backed up onto it which are themselves all encrypted and password protected. Mar 28, 2023 at 8:27
  • great, glad it worked for you. check updated answer for SP online. Mar 28, 2023 at 8:37
  • I get the error >! new-object : Cannot find an overload for "PSCredential" and the argument count: "2". At line:8 char:9 + $cred = new-object -typename System.Management.Automation.PSCredentia ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [New-Object], MethodException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand Mar 28, 2023 at 10:17
  • Try using $encpassword instead of $password for that like ($cred line) for check if it helps: stackoverflow.com/questions/22970505/… Mar 28, 2023 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.