Batch delete SHarePoint Online site collections in powershell from CSV

DWTK 61 Reputation points
2021-12-23T20:41:11.293+00:00

Helllo,

I have been trying to delete site collections for a pre migration clean up, My goal was to use a powershell script to get the site URLs to be deleted;

$sites= Import-Csv C:\AxiomSLDELETESiteListTEST.csv  
ForEach ($s in $sites)  
{  
Remove-SPOSite -Identity $s.SiteUrl -Confirm:$False  
}  

However when I run this on a test CSV with 5 actual URLs I get the following error on all 5

Remove-SPOSite : This site belongs to a Microsoft 365 group. To delete the site, you must delete the group.
At line:5 char:1

  • Remove-SPOSite -Identity $s.SiteUrl -Confirm:$False
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : NotSpecified: (:) [Remove-SPOSite], ServerException

160166-image.png

Is there another way to accomplish this, what am I doing wrong?

The CSV has one column title SiteURL

I appreciate any and all help.

DWTK

Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yi Lu_MSFT 17,616 Reputation points
    2021-12-24T03:25:08.15+00:00

    Hi @DWTK
    Microsoft 365 Groups are group objects that are available across Microsoft 365 services, you need to be assigned permissions before you can run this cmdlet.

    You could delete the modern team site and associated groups as below using PnP PowerShell.

        $tenantName= "companyName"  
        $username = "******@companyName.be"  
        $password = "SuperSavePassword" | ConvertTo-SecureString -AsPlainText -Force  
        $cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password  
          
        Connect-PnPOnline "https://tenant-admin.sharepoint.com" -Scopes "Group.ReadWrite.All", "Directory.ReadWrite.All" -Credentials $cred  
        $unifiedGroup = Get-PnPUnifiedGroup -Identity "Group Name"  
        Remove-PnPUnifiedGroup -Identity $unifiedGroup.ID   
        Disconnect-PnPOnline  
          
        Connect-PnPOnline "https://tenant-admin.sharepoint.com" -Credentials $cred  
        Remove-PnPTenantSite https://tenant.sharepoint.com/sites/ModernTeamSite  
        Disconnect-PnPOnline  
    

    For more information, you could refer to:
    https://winsmarts.com/completely-delete-an-office365-site-collection-classic-or-modern-from-recycle-bin-ce04808894fa

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    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.