Emptying the Recycle Bin via CSOM and PowerShell

This post shows how to empty a site collection’s recycle bin in SharePoint 2013 via CSOM wrapped in PowerShell – very useful if your site has exceeded the storage quota and you are locked out.


When a SharePoint site collection exceeds its storage quota, it’s a very nasty experience. Rather than informative error messages, users are essentially completely locked out of the Web UI, receiving 401 unauthorized errors. Even site collection administrators are locked out, and cannot get to any of the site administration pages. Admins then need to figure out, is it a broken master page issue? Did someone screw up permissions? Is it a draft publishing issue for the home page?

In this case, the remedies are to either increase the storage quota, or delete items from the site (including the recycle bin) to free up space.

This post shows a script I wrote to help delete items from the recycle bin, useful for the quota-exceeded scenario, or to clean up a site for archiving.

The Code

The Microsoft.SharePoint.Client.Site object has the RecycleBin property, which returns the collection of items in the bin, and the DeleteAll() method to empty it. I created two scripts to accomplish this – one for a single site with the ability to preview the items in the bin, and the other for bulk scenarios. In each script you can comment or uncomment the lines to use SPO authentication for multi-tenant, or Windows authentication for SPO-D or On-Prem.

Single Site Script

This script runs against a single site, and enables you to export the items to CSV to preview them, before performing the delete.


You can use the bulk version of the script to perform this on multiple sites. The bulk version doesn’t support previewing the results, and will just simply delete, so be sure you have your urls correct and know what you are deleting before running it. Bulk script accepts URLs passed as an array parameter, or can accept URLs passed as pipeline input from a CSV file. For CSV, you can pipe from the Import-CSV command to the script – just make sure you have a column in the CSV with a header named “Urls”.