Advertisements

Programmatically emptying the web and site Recycle Bins


I am currently working on a project, where I am migrating data from SQL into a SharePoint list. In doing so, I need to run this console utility through its paces, so, I have the need to constantly fill and purge the list several times.

Looping through each item within the list, for several thousand items is painstakingly slow however, so, I did find this great piece of code to do the job for me, which batch deletes all of the items from the specified list: http://code.msdn.microsoft.com/SharePointListDelete

Works exactly as advertised, and will now forever sit in my SharePoint tool belt. However, this method moves those items into the Recycle Bin. So, I thought I would share this with everyone else…

To empty both the web and site recycle bins, run the following code (modified for your environment of course…)

   1: using (SPSite site = new SPSite(SPContext.Current.Site.ID))
   2: {
   3:     using (SPWeb web = site.OpenWeb("/"))
   4:     {
   5:         // empty the web recycle bin
   6:         web.RecycleBin.DeleteAll();
   7:  
   8:         // empty the site collection recycle bin
   9:         site.RecycleBin.DeleteAll();
  10:     }
  11: }

 

Advertisements

About Geoff Varosky
Geoff Varosky is a Senior Architect for BlueMetal Architects, based out of Watertown, MA. He has been architecting and developing web based applications his entire career, and has been working with SharePoint for the past 13 years. Geoff is an active member of the SharePoint community, Co-Founder and Co-Organizer of the Boston Area SharePoint Users Group, co-founder for the Boston Office 365 Users Group, co-organizer for SharePoint Saturday Boston and speaks regularly at SharePoint events and user groups.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: