3

I'm attempting to delete a Site collection but receiving this error:

Microsoft.SharePoint.Client.ServerException: 'Cannot delete top-level site: https://{tenant}.sharepoint.com/teams/6KG9A4SO.'

    public static void DeleteSite(NetworkCredential nc, string siteToDelete)
    {
        using (var cc = new ClientContext(siteToDelete))
        {
            cc.Credentials = new SharePointOnlineCredentials(nc.UserName, nc.SecurePassword);

            // Delete the web object  
            cc.Web.DeleteObject();
            // Execute the query to the server.  
            cc.ExecuteQuery();
        }

    }

What am I doing wrong?

1

3 Answers 3

3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using System.Security;
using System.Threading;

namespace DeleteSiteCollection
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ClientContext tenantContext = new ClientContext("https://yoursite-admin.sharepoint.com/ Jump "))
            {
                SecureString password = new SecureString();
                foreach (char c in "yourpassword".ToCharArray())
                    password.AppendChar(c);
                tenantContext.Credentials = new SharePointOnlineCredentials("[email protected]", password);
                var tenant = new Tenant(tenantContext);
                SpoOperation spoOperation = tenant.RemoveSite("https://yoursite.sharepoint.com/sites/SiteA Jump ");

                tenantContext.Load(spoOperation);
                tenantContext.ExecuteQuery();

                Console.WriteLine("Time: " + DateTime.Now);
                while (!spoOperation.IsComplete)
                {
                    Thread.Sleep(3000);
                    tenantContext.Load(spoOperation);
                    tenantContext.ExecuteQuery();

                }
                Console.WriteLine("Time: " + DateTime.Now);
                Console.WriteLine("Verify that site no longer show up in getSiteProperties");
                try
                {
                    SiteProperties site = tenant.GetSitePropertiesByUrl("https://yoursite.sharepoint.com/sites/SiteA Jump ", false);
                    tenantContext.Load(site);
                    tenantContext.ExecuteQuery();
                    Console.WriteLine("Site is not deleted");
                }
                catch (ServerException e)
                {
                    Console.WriteLine("Cannot find the active site: " + e.Message);
                }
                Console.ReadLine();
            }
        }
    }
}
1
  • Excellent answer. Nice verification logic too Oct 17, 2017 at 15:01
2

Delete SharePoint Site collections programmatically using CSOM with PowerShell on SharePoint online / office 365.

function DeleteSiteCollection(){  
try{  
    $tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)  
    $removeSite = $tenant.RemoveSite("https://abc.sharepoint.com/sites/CustomTest")  
    $ctx.Load($removeSite)          
    $ctx.ExecuteQuery()  

    Write-Host "Site Collection has been deleted"        
    Write-Host "----------------------------"  
}  
catch{  
     write-host "$($_.Exception.Message)" -foregroundcolor red  
}   
} 

Note: The above set of operations needs to be called to get it executed.

DeleteSiteCollection #Deletes site collection using url

Reference Link 1, Reference Link 2

1

You are trying to delete the root web of your site collection.

You should attempt to delete the entire site collection, you can do this via PowerShell remove-sposite by connection to the https://tenant-admin.SharePoint.com

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.