PnP core component contains the extension methods which help to achieve the SharePoint task in simple lines of code. So to know more about PnP Core component, have a look at “Introduction to PnP Core Component”.
WebExtensions.DeleteWeb (PnP Core Component) – This extension method first checks the subsite available in the specified url and then delete that subsite from the parent web.
Method
boolDeleteWeb(stringsubsiteUrl)
Parameters:
| Parameter | Type | Description |
| subsiteUrl | string | A string that represents the URL leaf name of a subsite |
| Return Type | Description |
| bool | true if the web was deleted; otherwise false if nothing was done |
Syntax:
Single Line Code: Web.DeleteWeb("subsiteurl")
Code Snippet
The following example deletes the subsite from the parent website based on the given url and returns true value. If the subsite is not available or there's a problem in deleting the child site, this method returns false.
- Create a console application.
- Add PnP core component assembly references to the application. Refer “Introduction to PnP Core Component” to add a references to the VS solution.
- Add Using Microsoft.SharePoint.Client and Using OfficeDevPnP.Core.Extensions as using statement to the top of the file.
- Add the below code snippet and change the values where ever necessary.
- using System;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Threading.Tasks;
- usingMicrosoft.SharePoint.Client;
- usingOfficeDevPnP.Core;
- // Assembly Reference Used: OfficeDevPnP.Core, Version=2.4.1605.0, Culture=neutral, PublicKeyToken=3751622786b357c2
- //Supports SharePoint Online, SharePoint 2013+
- namespaceSampleApplication
- {
- classProgram
- {
- staticvoid Main(string[] args)
- {
- stringsiteUrl = "https://sharepointonline.sharepoint.com";
- AuthenticationManagerauthManager = newAuthenticationManager();
- //Interactive Login to SharePoint site - Opens a Online signin page to authenticate the user
- var context = authManager.GetWebLoginClientContext(siteUrl);
- //Deletes the child website with the specified subsite URL, from a parent Web, if it exists. Else the method return false value.
- bool success = context.Web.DeleteWeb("subsiteurl");
- if (success)
- Console.WriteLine("Site deleted successfully.");
- else
- Console.WriteLine("Problem in deleting the site.");
- Console.WriteLine("Press any key to exit.");
- Console.ReadKey();
- }
- }
- }

Join the conversation! Your thoughts help the community grow.