Delete Microsoft Team Instantly

Introduction

There are a couple of ways to delete an MS team. If you are the owner, you can delete a team using the MS Desktop client for Teams, or if you have a Teams Administrator role, you can delete it from the Teams admin interface or by running a PS command. I prefer using the PS command, as it has its own advantages. First, you have a trace of the runbook of all the operations you are doing, and it is easy to track. Second, you don’t need to wait for the MSFT scheduled job to run to delete the required dependencies. Once a team is created, it has an associated SPO site and M365 Group. Points to note.

  • Deleted Group objects take 30 days to clean up from Azure AD.
    Deleted Group
  • Deleted Sites from Tenant recycle bin takes 93 days to clean up.
    Deleted Sites
  • Kindly note that once the script has run, this cannot be undone. The Team and all its associated objects are deleted for good. There is no way to recover them.

Why delete a team instantly?

There could be a couple of reasons, such as,

You might want to recreate the Team with the same name and should have the same group email address.

Pre-Requisites

  • Your admin account should have Teams admin and SharePoint admin rights. Check the role in privileged admin roles in Microsoft Entra (https://portal.azure.com).
  • You need to have the app registration in Azure Portal and have the API permissions set up with application type ‘Delegated’. Please refer to the references section to set up the application registration. This is a very important step, so follow the steps from the reference link.
  • You need to have PnP PowerShell modules installed. This script was tested against the version 2.12.0.
    PnP PowerShell modules

Steps

Follow the below instructions to delete a team completely from the tenant.

Step 1. Define the required variables and parameters.

# Replace the Client ID with respect to the APP ID configured for your org
$PnPClientId = "qa123486-baf5-4274-b2a0-4a9256398640"

# Replace the Admin URL with respect to your organization
$SPOAdminUrl = "https://contoso-admin.sharepoint.com"

# Prompt user for the SPO site URL associated with Team
$SiteUrl = Read-Host -Prompt "Please enter the SPO site URL associated with Team"

Step 2. Connect to SharePoint Admin URL using PnP PowerShell modules and store the connection in a variable. Since you are using interactive, your account will be asked to log in with a popup window.

$SPOAdminConnection = Connect-PnPOnline `
    -Url $SPOAdminUrl `
    -Interactive `
    -ClientId $PnPClientId `
    -ReturnConnection

Step 3. Delete the Team first. Then delete the Deleted Group and then perform the associated Deleted SPO Site cleanup. Below are the commands to be executed in order.

# Get the group ID    
$SiteInfo = Get-PnPTenantSite `
    -Identity $SiteUrl `
    -Connection $SPOAdminConnection | Select *

$GroupID = $SiteInfo.GroupId.Guid

# Delete Team    
Remove-PnPTeamsTeam `
    -Identity $GroupID `
    -Connection $SPOAdminConnection `
    -Force

# Remove Deleted Group    
Remove-PnPDeletedMicrosoft365Group `
    -Identity $GroupID `
    -Connection $SPOAdminConnection

# Remove Deleted Site
Remove-PnPTenantDeletedSite `
    -Identity $SiteUrl `
    -NoWait `
    -Connection $SPOAdminConnection `
    -Force

Complete Script

# Load the latest PnP PowerShell module
# Get the Group ID from the URL
# Delete the Group associated with teams
# Delete the Group from deleted groups
# Find the deleted sites from SPO Admin Center
# Elevate SPO Admin rights for your csec account
# Deleted group takes 30 days to completely clean up
# Deleted sites takes 93 days to completely clean up

# Replace the Client ID with respect to the APP ID configured for your org
$PnPClientId = "qa12343686-baf5-4274-b2a0-4a9256398640"

# Replace the Admin URL with respect to your organization
$SPOAdminUrl = "https://contoso-admin.sharepoint.com"

# Prompt user for SPO site URL associated with Team
$SiteUrl = Read-Host -Prompt "Please enter the SPO site URL associated with Team"

try {
    if (!$SPOAdminConnection) {
        # Connect to SPO Admin Center using PnP PowerShell
        $SPOAdminConnection = Connect-PnPOnline -Url $SPOAdminUrl -Interactive -ClientId $PnPClientId -ReturnConnection
    }
   
    # Retrieve site information for the specified site URL
    $SiteInfo = Get-PnPTenantSite -Identity $SiteUrl -Connection $SPOAdminConnection | Select *

    # Get the Group ID associated with the site
    $GroupID = $SiteInfo.GroupId.Guid

    # Deleting the team associated with the site
    Write-Host "Deleting team $($SiteInfo.Title)" -ForegroundColor Yellow
    Remove-PnPTeamsTeam -Identity $GroupID -Connection $SPOAdminConnection -Force
    Write-Host "Team $($SiteInfo.Title) deleted successfully!" -ForegroundColor Green

    # Deleting the associated deleted group for the team
    Write-Host "Deleting the associated deleted group for team $($SiteInfo.Title)" -ForegroundColor Yellow
    Remove-PnPDeletedMicrosoft365Group -Identity $GroupID -Connection $SPOAdminConnection
    Write-Host "'Deleted Group' associated with team $($SiteInfo.Title) has been cleaned up successfully" -ForegroundColor Green

    # Removing the deleted sites in the tenant recycle bin associated with the team
    Write-Host "Working on removing the deleted sites in tenant recycle bin associated with team $($SiteInfo.Title)" -ForegroundColor Yellow
    Remove-PnPTenantDeletedSite -Identity $SiteUrl -NoWait -Connection $SPOAdminConnection -Force
    Write-Host "'Deleted Site' associated with team $($SiteInfo.Title) has been cleaned up successfully" -ForegroundColor Green    
}
catch {
    # Handle terminating exception if one occurs
    Write-Host "Error Occurred while deleting team: $($_.Exception.Message)" -ForegroundColor Red
}

Conclusion

Thus, in this article, we have seen how to delete an MS Team instantly without waiting for the MSFT scheduled clean-up jobs.

References