Delete SharePoint Online site collections using powershell

Hoke 1 Reputation point
2022-05-10T13:22:58.1+00:00

Hello,

I have been trying to delete site collections from a csv, I'm trying to use a powershell script to get the site URLs to be deleted.

I've tried everything, now that I've run out of ideas I'm opening this question. I'm accepting possibilities other than Powershell

PS: I'm trying to do this without creating anything in the tenant.

This site belongs to a Microsoft 365 group. To delete the site, you must delete the group.

200674-screenshot-57.png

Below are some attempts:

#Modules  
Import-Module ExchangeOnlineManagement  
Import-Module Microsoft.Online.SharePoint.PowerShell  
Import-Module SharePointPnPPowerShellOnline  
      
$site = "https://tenant.sharepoint.com/sites/site"  
$mailGroup = "******@onmicrosoft.com"  
      
#First  
Remove-UnifiedGroup -Identity $mailGroup-Confirm:$false  
Remove-SPOSite -Identity $site -NoWait -Confirm:$false  
      
#Second  
Remove-UnifiedGroup -Identity $mailGroup-Confirm:$false  
      
Set-SPOSite -Identity $site  -LockState "unlock"   
Set-SPOSite -Identity $site  -Owner $userCredential.UserName  
Remove-SPOSite -Identity $site -NoWait -Confirm:$false  
      
#Third  
$SharepointSite = Get-SPOSite $site  
Remove-PnPUnifiedGroup -Identity $SharepointSite .GroupID  
Remove-PnPTenantSite $site  

I also tried what is in the link: https://learn.microsoft.com/en-us/answers/questions/674248/batch-delete-site-collections-in-powershell-from-c.html

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. Xuyan Ding - MSFT 7,601 Reputation points
    2022-05-12T10:35:58.2+00:00

    Hi @Hoke ,

    You could delete the group associated with the site collection. After deleting the Microsoft 365 group, you should wait for a while and the site will be automatically deleted.

    PowerShell command:

    #Parameters  
    $AdminCenterURL="https://tenant-admin.sharepoint.com"  
    $GroupSiteURL="https://tenant.sharepoint.com/sites/site"  
      
    #Connect to SharePoint Online  
    Connect-SPOService -Url $AdminCenterURL  
      
    #Unlock the Group site Collection  
    Set-SPOSite -Identity $GroupSiteURL -LockState unlock  
      
    Remove-UnifiedGroup -Identity "group name"  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.