SPSite and the Recycle Bin in SharePoint 2010 SP1

Service Pack 1 for SharePoint 2010 brought us a much-needed feature – the recycle bin for SPWeb and SPSite objects.

Recycling SharePoint Sites

If you want to move an SPWeb into the recycle bin programmatically, there’s the fairly-straightforward SPWeb.Recycle() method. Once you’ve invoked this, you can restore the SPWeb from the SPSite.RecycleBin object with the Restore() method.

But, how do I recycle and restore a site collection (SPSite)?

Recycling SharePoint Site Collections

Firstly, there’s no Recycle() method on the SPSite object. Invoking Delete() removes the SPSite immediately, unless you pass in an argument to get it to perform a gradual delete. The gradual delete process is carried out by the “Gradual Delete Timer Job”, whose purpose is to delete site collections efficiently 1000 database rows at a time.

However, it also has some nice side effects. The timer job only deletes site collections that were deleted more than 30 days ago (or whatever the recycle bin age is configured for the web application). In that intermediate time, the deleted site collections are available to be restored.

You can get at the deleted sites via the GetDeletedSites method of the SPWebApplication object. This returns a collection of SPDeletedSite objects, on which you can invoke the Restore method.

Sample Code

// get our web application
SPWebApplication webapp = SPWebApplication.Lookup(new Uri("http://sharepoint"));
// find our deleted site collection
foreach (SPDeletedSite site in webapp.GetDeletedSites("http://sharepoint/sites/myoldsite"))
    //restore it!


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>