I am trying to delete site collections using CSOM script. Below is the code snippet :

using (ClientContext tenantContext = new ClientContext(siteURL))
    tenantContext.Credentials = new NetworkCredential(userName, password, domain);//new SharePointOnlineCredentials(@"dom\ishan", password);
    var tenant = new Tenant(tenantContext);
    SpoOperation spoOperation = tenant.RemoveSite(siteURL);

I am getting an error as shown below, while trying to tenantContext.ExecuteQuery();

An unhandled exception of type 'Microsoft.SharePoint.Client.ServerUnauthorizedAccessException' occurred in Microsoft.SharePoint.Client.Runtime.dll

Additional information: Access denied. You do not have permission to perform this action or access this resource.

Is there any specific permissions required on farm to work with Tenant objects?

NOTE : I am Site Collection Admin and i am able to delete the site collection through UI.


To work with the tenant in CSOM, you must have the SharePoint administrator role or the Global Administrator role in Office 365.

There is a command in the SharePoint Online Management shell to do this for you, Remove-SPOSite.

  • thanks for your response. I do have admin role at the web app level, is that not enough? I am able to see all the site collections and can go through their settings without being added as Admin. – Ishan Dec 1 '15 at 15:50
  • then that should be sufficient, updated with a direct way to do it in the Powershell module for SharePoint Online. – Eric Alexander Dec 1 '15 at 16:01
  • Although i am the global administrator for O365 tenanat , and getting the tenantContext , giving me access denied exception. – user27178 Aug 29 '16 at 12:56

you need a username with permissions to remove a site on the site collection. which I believe is Global or SharePoint Online admin.

Here is very nice code from technet gallery

Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  

$siteUrl = “https://mytenant.sharepoint.com/sites/mysitecollection/site1” 
$username = "admin@mytenant.onmicrosoft.com" 
$password = Read-Host -Prompt "Enter password" -AsSecureString  
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)  
$ctx.Credentials = $credentials 

$web = $ctx.Web  


Write-Host $web.Title "Site Deleted" 
  • I am using AD authentication and i do not have online credentials. Do i need to request those separately from Microsoft Support? – Ishan Dec 1 '15 at 15:53
  • do you have the Sharepoint online admin permission or Tenant admin rights? that is what you need. try the above code with those account – Waqas Sarwar MVP Dec 1 '15 at 15:56

The technique is bit different to access SharePoint Online Credential than onpremises Credential

Authenticate and authorize your credentials this way, it will work

using (ClientContext tenantContext = new ClientContext("siteUrl For your Sponlinesite"))
    SecureString passWord = new SecureString();
    foreach (char c in "password".ToCharArray())
         tenantContext.Credentials = new SharePointOnlineCredentials("UserName", passWord);

         PeopleManager peopleManager = new PeopleManager(tenantContext);
  • I had already tried this, this does not work with AD credentials. – Ishan Dec 2 '15 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.