Empty SharePoint Recycle Bins Programmatically with PowerShell

Requirement: Empty recycle bin in SharePoint
In a Database attachment method migration project, we decided to clear all the recycle bins to get some additional free space in databases. There were 3000+ site collections. Automation?

SharePoint Automation = PowerShell 🙂

how to empty sharepoint recycle bin using powershell

Empty SharePoint Site Collection Recycle bin using PowerShell:

The Recycle bin in SharePoint is a container used to retain deleted items such as list items, documents, document libraries, lists, and sites to a number of days defined by the SharePoint administrator. Deleted items in recycle bin can be restored if needed. Here is the PowerShell script to empty SharePoint recycle bin.

#Get the Site Collection
$Site = Get-SPSite "https://portal.crescent.com/sites/test"

#Delete all from 1st Stage Recycle bin
$Site.AllWebs | Foreach-object { $_.RecycleBin.MoveAllToSecondStage() }

#Empty 2nd Stage Recycle bin
$Site.RecycleBin.DeleteAll();

This deletes all items from recycle bin in SharePoint 2013 using PowerShell.

PowerShell Script to Empty SharePoint Recycle Bins Programmatically:

Let’s empty the recycle bin of all site collection under a given web application. Here is the PowerShell to delete all items in recycle bin.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

$WebApp=Get-SPWebApplication "http://intranet.crescent.com"

foreach ($SPSite in $webApp.Sites)
{
	#get the collection of webs
	foreach($SPWeb in $SPSite.AllWebs)
	{
		#Empty the 1st Stage Recycle bin items PERMENANTLY
		#$SPWeb.RecycleBin.DeleteAll();

		#Send the 1st Stage Recycle bin items to 2nd Stage
		$SPWeb.RecycleBin.MoveAllToSecondStage();

		write-host "End-User Recycle Bin Items Deleted for:" 
		write-host $SPWeb.title ":" $SPWeb.URL "`n"

		#Dispose Web object
		$SPWeb.Dispose()
	}
	#Empty SharePoint site collection recycle bin (Second Stage Recycle bin) or Admin Recycle bin
	$SPSite.RecycleBin.DeleteAll();

	#Dispose Site object
	$SPSite.Dispose()

	write-host "Administrator Recycle bin Items Deleted for:" $SPSite.RootWeb.title "`n"
}

Above code is fairly simple, and can be rewritten in C# also.

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!

12 thoughts on “Empty SharePoint Recycle Bins Programmatically with PowerShell

  • September 10, 2020 at 11:56 PM

    Thanks so much. Work great for me. thousand of items and take a while but completed .. save so much time ..

    Reply
  • September 14, 2019 at 12:50 AM

    can we delete particular list from the recycle bin which has 1 gb size using powershell script. if any scripts please share in the comment section

    Reply
  • August 22, 2018 at 7:57 PM

    Found it:

    Add-PSSnapin “Microsoft.SharePoint.PowerShell”

    #enter deleted by name as it is not james.bond or with domain included or else you will have to use -like
    $recyclebinitems = (Get-SPSite “http://sitecollection.com”).RecycleBin | where {$_.DeletedByName -match “james bond”}

    foreach ($file in $recyclebinitems) {

    Write-Output “Deleting file $($file.Title)…”

    (Get-SPSite “http://sitecollection.com”).RecycleBin.Delete($file.ID)

    start-sleep -s 1

    }

    Reply
  • August 16, 2018 at 5:46 PM

    Salaudeen,
    Thank you for the reference. I cannot get it to work, it throws error displaying “You cannot call a method on a null-valued expression”. Can you post the one that filters by user who deleted and then delete from all places?

    Reply
  • August 15, 2018 at 10:56 PM

    Hi Salaudeen,
    Can we delete files from recycle bin if deleted by certain user?

    Reply
  • May 20, 2016 at 8:20 PM

    Can this be modified to run on a single site collection and not the entire Web App?

    Reply
  • July 9, 2015 at 7:34 AM

    Nice script, thanks for sharing.
    But I have to note that it leaks memory as it does not dispose the SPSite and SPWeb objects. Details can be found here: https://msdn.microsoft.com/de-de/library/aa973248(v=office.12).aspx

    Reply
  • September 17, 2014 at 5:01 PM

    How do you run this Powershell in Sharepoint 2007?

    Reply

Leave a Reply