0 out of 1 rated this helpful - Rate this topic

SPWeb.Delete Method

Deletes the website.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
[ClientCallableExceptionConstraintAttribute(FixedId = "SPException$-2146232832", ErrorType = typeof(SPException), 
	Condition = "The current site is the top level site.", ErrorCode = )]
[ClientCallableMethodAttribute(Name = "DeleteObject", RemoveThisObjectFromParentCollection = true)]
public void Delete()
Exception Condition
SPException

The deleting website is a root website.

To delete a site collection, use the Delete method.

The following code example deletes a specified website.

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

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

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Microsoft.SharePoint.SPException : You can’t delete a site that has subsites.
If you're getting the above error, then you need to delete all the subsites first. There's no built-in way to do this, so I've put together an extension method to do it for you: http://tsstsst.com/2012/02/spfix-deleting-a-site-with-subsites-programmatically/
You can't delete a site that has subsites
Delete the subsites first.