PowerShell script to empty SharePoint Recycle Bin

If you have lots of stuff in your SharePoint Recycle Bin and want to empty it all, clearing the bin from the user interface would cause the browser to time out, following is how to create a PowerShell command that will do the job for you.

To use the script

  • In the script below: change the variable $sitecollection to your site collection value
    • $sitecollection=“http://myserver/sites/mysitecollection/”
  • Open PowerShell console, paste the below script and then execute

Depending on the number of rows in the RecycleBin, the purge may take a while.

PowerShell Script

 

Add-PSSnapin
Microsoft.SharePoint.PowerShell -EA 0

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)#$batchsize=10


function
OutHorRuler

{


write-host
“—————————————————————–“

}

 

function
DisplayTimeStamp

{


$a=get-date


write-host
$a.ToLongTimeString()

}

 

###############################################################################


#


#function to loop through items in the Recycle Bin and purge them using DeleteAll


#


# $ListURL=the absolute URL to a site or web


# $batchSize=number of items queried in every iteration


# $stage=specifies the stage of the recyle bin 1 or 2


#


###############################################################################

 

function
PurgeRecycleBinUsingDeleteAll

 

{param ($ListURL,$batchSize,$stage)

 

$mySite
=
new-object
Microsoft.SharePoint.SPSite($ListURL)


$myWeb
=
$mySite.OpenWeb()


Write-host
“Intializing SPWeb, SPSite for “
$ListURL


OutHorRuler

 

$itemCount=0


$recylebinQuery=new-object
Microsoft.SharePoint.SPRecycleBinQuery


$recylebinQuery.RowLimit =
$batchSize

 

write-host
“Going to delete all items using DeleteAll in the recycle bin with batch size: “
$batchSize


$loop=1


While ($loop
-gt 0)

{


write-host
“@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@”


DisplayTimeStamp


write-host
“Begin query”


$itemColl=$myWeb.GetRecycleBinItems($recylebinQuery)


$loop=$itemColl.Count

 

$itemColl.DeleteAll()


write-host
“DeleteAll complete”


DisplayTimeStamp


write-host
“@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@”

}

 

write-host
“About to dispose SP objects”


$myweb.Dispose()


$mySite.Dispose()

}

 

###############################################################################


#


#function to loop through items in the Recycle Bin and purge them using DeleteOneByOne


#


# $ListURL=the absolute URL to a site or web


# $batchSize=number of items queried in every iteration


# $stage=specifies the stage of the recyle bin 1 or 2


# $deletemode=specifies whether to do a DeleteAll(=2) or Delete one by on(=1)


#


###############################################################################

 

function
PurgeRecycleBinUsingDeleteOneByOne

 

{param ($ListURL,$batchSize,$stage,$deletemode)

 

$mySite
=
new-object
Microsoft.SharePoint.SPSite($ListURL)


$myWeb
=
$mySite.OpenWeb()


Write-host
“Intializing SPWeb, SPSite for “
$ListURL


OutHorRuler

 

$itemCount=0


$recylebinQuery=new-object
Microsoft.SharePoint.SPRecycleBinQuery


$recylebinQuery.RowLimit =
$batchSize


#if ($stage -eq 1)


# {


# $recylebinQuery.ItemState=[Microsoft.SharePoint.SPRecycleBinItemState]::FirstStageRecycleBin


# }


#else


# {


# $recylebinQuery.ItemState=[Microsoft.SharePoint.SPRecycleBinItemState]::SecondStageRecycleBin


# }

 

$starttime=get-date


write-host
“Going to delete all items OneByOne in the recycle bin with batch size: “
$batchSize


$totalcount=[system.int32]0


$loop=1


While ($loop
-gt 0)

{


write-host
“@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@”


DisplayTimeStamp


write-host
“Begin query”


$starttimeForThisLoop=get-date


if ($stage
-eq 1)

{


$recylebinQuery.ItemState=[Microsoft.SharePoint.SPRecycleBinItemState]::FirstStageRecycleBin


$itemColl=$myWeb.GetRecycleBinItems($recylebinQuery)

}


else

{


$recylebinQuery.ItemState=[Microsoft.SharePoint.SPRecycleBinItemState]::SecondStageRecycleBin


$itemColl=$mySite.GetRecycleBinItems($recylebinQuery)

}

 

write-host
“End query”


DisplayTimeStamp

 

$loop=$itemColl.Count


write-host
“Got the count of items to delete:”
$loop


DisplayTimeStamp


if ($deletemode
-eq 1)

{


write-host
“Performing Item by Item deletion”


foreach ($itemtodelete
in
$itemColl)

{


$idsarray
= [System.Guid]$itemtodelete.ID


$itemColl.Delete($idsarray)

}

}


else

{


write-host
“Performing DeleteAll”


$itemColl.DeleteAll()

}

 

write-host
“Finished deletion”


DisplayTimeStamp

 

$totalcount=$totalcount+$loop


$endtime=get-date

 

$span=[System.timespan]($endtime

$starttime)


$timeelapsed=$span.TotalSeconds


$rateofdelete=$totalcount/$timeelapsed*60

 

$spanForThisLoop=[System.timespan]($endtime

$starttimeForThisLoop)


$timeelapsedForThisLoop=$spanForThisLoop.TotalSeconds


$rateOfDeleteForThisLoop=$loop/$timeelapsedForThisLoop*60


write-host
“OneByOneDelete complete, items deleted:”
$totalcount
” stage:”
$stage
” deletemode:”
$deletemode


write-host
“Average rate of deletion/min:”
$rateofdelete


write-host
“Current rate of deletion/min:”
$rateOfDeleteForThisLoop


DisplayTimeStamp


write-host
“@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@”

}

 

write-host
“About to dispose SP objects”


$myweb.Dispose()


$mySite.Dispose()

}

 

write-host
$timeelapsed

 

????$batchsize=100

 

$deletemode=2

 

$sitecollection=“http://myserver/sites/mysitecollection/”

 

DisplayTimeStamp

 

write-host
“clear the first stage”


PurgeRecycleBinUsingDeleteOneByOne
$sitecollection
$batchsize 1 $deletemode

 

write-host
“clear the second stage”


PurgeRecycleBinUsingDeleteOneByOne
$sitecollection
$batchsize 2 $deletemode

 

DisplayTimeStamp

About the Author

Dany Hoyek
I am a Computer and Communication Engineer and Consultant. I have been leading, developing and managing millions of dollars worth of software development projects.