how to delete SharePoint Group connected sites using PowerShell?

Henrietta Mueller 0 Reputation points
2024-03-12T12:37:41.11+00:00

Hi members, In SharePoint, I need to delete a SharePoint Group connected sites through PowerShell, but i was facing an error

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

error

Anyone please suggest me how to delete SharePoint Group connected sites using PowerShell?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,653 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,673 questions
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,806 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,377 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,068 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Emily Du-MSFT 41,786 Reputation points Microsoft Vendor
    2024-03-13T04:37:48.7233333+00:00

    When you delete a Microsoft 365 group, all group resources, including SharePoint site, mailbox conversations, Teams, notebook, and Planner tasks also get deleted. So, to delete a SharePoint group connected site, you must need to delete the Microsoft 365 group first.

    Here are four methods to delete a Microsoft 365 group.

    1.Go to Microsoft 365 admin center -> Active teams &groups -> Delete the Microsoft 365 group.

    2.Use PNP PowerShell.

    $AdminSiteURL = "https://tenant-admin.sharepoint.com"
    $GroupEmail = "Group@tenant.onmicrosoft.com"
     
    Connect-PnPOnline -Url $AdminSiteURL -Interactive
     
    $Group = Get-PnPMicrosoft365Group | Where Mail -eq $GroupEmail
     
    Remove-PnPMicrosoft365Group -Identity $Group.id
    Write-host "Group Deleted Successfully!" -f Green
    

    3.Use Exchange PowerShell.

    Connect-ExchangeOnline -ShowBanner:$False
     
    Remove-UnifiedGroup -Identity "Group@tenant.onmicrosoft.com"
     
    Disconnect-ExchangeOnline -Confirm:$False
    

    4.Use Azure AD PowerShell.

    #Parameters
    $GroupEmail = "Group@tenant.onmicrosoft.com"
     
    #Connect to AzureAD
    Connect-AzureAD -Credential (Get-Credential) | Out-Null
     
    #Get the Azure AD Group
    $Group  = Get-AzureADGroup -Filter "Mail eq '$GroupEmail'"
     
    If($Group)
    {
        $Prompt = Read-Host "Are you sure want to delete the Group (Y/N)?"
        If($Prompt -eq "Y")
        {
            #Delete the Office 365 Group
            Remove-AzureADGroup -ObjectId $Group.ObjectId
            Write-Host "Group Deleted Successfully!" -f Green
        }
    }
    

    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.


  2. Ganesh Sanap 206 Reputation points MVP
    2024-03-17T08:58:07.7566667+00:00

    You can use PnP PowerShell or CLI for Microsoft 365 to easily delete Microsoft 365 group and SharePoint site associated with it.

    Example:

    Using PnP PowerShell:

    Remove
    

    Using CLI for Microsoft 365:

    m365 entra m365group 
    

    References:

    1. Remove-PnPMicrosoft365Group
    2. m365 entra m365group remove
    3. Delete all Microsoft 365 groups and SharePoint sites using PnP PowerShell
    4. Delete all Microsoft 365 groups and SharePoint sites using PnP PowerShell

    Important Note: Link #3 and #4 given above deletes all M365 groups and SharePoint sites associated with. DO NOT run those scripts as is in your environment. I have provided those links just for reference to see how to use those commands. You will have to run the specific commands for only one site/M365 group.


    If the answer is helpful, please click Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click Comment. For SharePoint/Power Platform blogs, visit: Ganesh Sanap Blogs.


  3. Craig Gregory 0 Reputation points
    2024-03-21T03:05:13.3+00:00

    Henrietta,

    I have been working on this for about week and a half. Your question was perfect timing. I'm working on cleaning up our inactive SharePoint sites that have had no activity for over 2 years and beyond. What I have utilized is the OOTB SharePoint Admin Center Active sites export and then also running a PowerShell that gets the GroupId's for the sites.

    With some help from MSFT, forums, chatgpt, from this post, and lots of trial and error, for not a developer and Powershell I'm very new to, got a working solution. It does its job but does kick some errors, but seem to be false for the group and sites get deleted. Does take a bit for it to show up in the Deleted sites in SharePoint Admin center but does work. Did a test run of 10 sites and all ended up in Deleted Sites recycle bin.

    Here is what I have and hope it helps. Sharing is Caring!

    Also adding the disclaimer that this goes without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose.

    #Insert your org name between the quotes below

    $orgname="orgnameinsert"

    Connect-pnponline https://$orgname-admin.sharepoint.com -interactive

    #Import Excel sheet table

    $excelFilePath = "C:\x\x\filename.xlsx"

    $table = Import-Excel -Path $excelFilePath

    Format table with URL and GroupID these are my headers in the Excel table

    $formattedTable = $table | Select-Object URL, GroupID

    Remove Group if SharePoint site is tied to group, then delete site, if not tied to Group then delete site

    $formattedTable | ForEach-Object {

    $url = $_.URL
    
    $groupId = $_.GroupID
    
    # Verify if the site URL is tied to a Group
    
    $site = Get-PnPTenantSite -url $url
    
    if ($site.Template -eq "Group#0") {
    
        # If the site is tied to a group remove group and send to recycle bin
    
        Remove-PnPMicrosoft365Group -Identity $groupId
    
        # Wait 60 to 120 seconds to allow group to be deleted
    
        Start-Sleep -Seconds 120
    
        # Remove the SharePont site
    
        Remove-PnPTenantSite -Url $url -Force
    
    } else {
    
        #Remove the SharePoint site not tied to a group
    
        Remove-PnPTenantSite -Url $url -Force
    
    }
    

    }

    0 comments No comments