Question

I have a site which has site policy associated.

enter image description here

based on my policy site is closed and marked as read-only.

When I try to delete the site from Admin got an error "Couldn't delete site"

enter image description here

One of the possible way to delete the site is, first Open the site from Site Closure and Deletion and then delete. Is there any other way I can delete the site without opening?

Is there CSOM API which with which I can open the site?

Was it helpful?

Solution 2

In order to delete the site I had to first Open the site.

    using (ClientContext clientContext = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(url, <ClientID>, <ClientSecret>))
    {
        var web = clientContext.Site.RootWeb;
        clientContext.Load(web);
        clientContext.ExecuteQuery();

        web.SetOpenBySitePolicy();
    }

    using (var clientContext = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(
                                    <TenantAdminUrl>
                                    , <ClientID>
                                    , <ClientSecret>))
    {
        var tenant = new Tenant(clientContext);

        var removeSite = tenant.RemoveSite(url);
        clientContext.Load(removeSite);
        clientContext.ExecuteQueryRetry();
    }

OTHER TIPS

You could delete the site by PowerShell command “Remove-SPOsite”.

There is PowerShell script for your reference.

#Variables for processing

$AdminSiteURL = "https://tenantname-admin.sharepoint.com/"

$AdminName = "username@tenantname.onmicrosoft.com"

$SiteCollURL = "https://tenantname.sharepoint.com/sites/Sales"



#Credentials to connect 

$SecurePWD = ConvertTo-SecureString "Password" –asplaintext –force  

$Credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $AdminName, $SecurePWD



#Connect to SharePoint Online

Connect-SPOService -url $AdminSiteURL -credential $Credential



#Delete SharePoint Online Site Collection

Remove-SPOSite -Identity $SiteCollURL -NoWait -Confirm:$false



Write-Host "Site collection has been deleted!"

For more detailed information, refer to the article below.

SharePoint Online: Delete Site Collection using PowerShell.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top