2

I recently got a comment on my Deleting a site with subsites programmatically article about properly disposing of SPWeb objects after they've been deleted:

Don’t forget to dispose the deleted web.

When I wrote the article I was convinced that it was disposed of by the SPWeb.Delete() method, but I can't find any reference or reason behind my certainty.

Is it disposed of automatically? Or have I been peddling bad code?

2 Answers 2

2

From this sample on msdn, I would said that dispose is still required.

using (SPSite oSiteCollection = new SPSite("http://Site_Name"))
{
   SPWeb oWebsite = oSiteCollection.AllWebs["DeleteWebSite"];

   oWebsite.Delete();
   oWebsite.Dispose();
}

I also had a check at the source for the Delete method. All I see is a call to SPRequest.Delete(string) by passing an url, so I really don't see any (simple) way it could perform automatical dispose. That said, it is possible that the deletion performed by the SPRequest class fires some event that in turn disposes all the "active" SPWeb istances (SharePoint seems to keep track of all of them internally), but... to quote the Guide... its just highly improbable.

EDIT: I have had a look to the implementation of the Delete method on both SP 2010 and 2007. It would seem that the main difference is the addition of the recycle flag to the SPRequest call. This would point to the SPWeb istance not begin disposed: think about it, you delete a web by sending it to the recycle bin and the istance is disposed? won't make much sense IMHO. Had also done a little test: deleted a web site and then called dispose. The log DIDN'T show the usual "Detected usage of previously closed ecc ecc" so we should be clear. The SPRequest method also doesn't seem to perform any of the operation performed by the SPWeb.Close() method (which is internally called by .Dispose).

As last evidence, I would also point that to my knowledge, the SharePoint Do NOT Dispose guidance doesn't include any reference to the web delete method, so we can safely assume that the istance is NOT disposed.

2

According to this example on MSDN, they have disposed the SPWeb object after deleting it.

After using ILSpy on the SPWeb.Delete() method, we can see that internally it calls the Microsoft.SharePoint.Library.SPRequest.DeleteWeb(string bstrUrl, bool bRecycle) method.

So from this we can infer that SPWeb.Delete() must be synonymous with deleting a site from the UI after which it is sent to the Recyle Bin from where it can be restored later.

SPWeb.Dispose() disposes the .NET object of that particular site while SPWeb.Delete() deletes the actual site.

1
  • I've upvoted both your answers. They're both pretty comprehensive, but SPA did some extra testing to make double-sure it was the case. :) Thanks for your answer.
    – Stu Pegg
    Nov 8, 2012 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.