Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
    
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. –  Stuart Pegg Nov 8 '12 at 14:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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