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.