I want to delete Site collection with all sub-sites using CSOM on SharePoint 2013 On-Prem. I've tried with web.DeleteObject()
but its works only for sub-sites not for top level site. Pls let me know if there is any other to achieve this way by using CSOM.
-
You can indeed delete Site Collections via csom. However, if the Site Collections have existing sub Sites, you may see an error.– Dave SmithNov 29, 2017 at 16:35
2 Answers
you cannot delete Site Collection using CSOM and you will have to rely on either PowerShell or Web Services. you can do it in on SharePoint-Online.
The account that accesses this Web service must be a member of the Farm Administrators group in the Central Administration site. By default, the server's local Administrators group is in the Farm Administrators group. To access the Admin service and its methods, set a Web reference to http://Server_Name:Port_Number/_vti_adm/Admin.asmx.
Source: Link
This works in SharePoint 2016
var ctx = new ClientContextWrapper(targetUrl);
var tenant = new Tenant(ctx);
var spoOperation = tenant.RemoveSite(targetUrl);
ctx.Load(spoOperation);
ctx.ExecuteQuery();
If you're in 2013, you may need some extra PowerShell steps to configure your farm - https://blogs.msdn.microsoft.com/vesku/2014/06/09/provisioning-site-collections-using-sp-app-model-in-on-premises-with-just-csom/