Remove Microsoft Teams Deleted Teams

A few weeks ago, I was playing with the Microsoft Teams creation using Microsoft Graph endpoint and as expected, a couple of teams were created because of that. Using these endpoints can be extremely useful to automate a process on our organization. However, when you’re doing this it will create a lot of “testing” teams. This is also applicable to Teams that are not “testing”.

Be aware there is no coming back after deleting all these Teams/SharePoint sites.

So, each Microsoft Teams that you create will create at the minimum an Office 365 group, a SharePoint site and Microsoft Teams Team.

  1. Deleting the Microsoft Teams
  2. Go to the Team option.
  3. Delete the team that you want to delete.

So, the first is done when you delete a team, the SharePoint site created by that team it will not be erased therefore we need to delete the SharePoint site that is connected to the Team.

  1. Deleting SharePoint site
  2.  Go to SharePoint admin centre
  3. Go to active site
  4. Select the site/s that you and delete.

Now everything is kind of “soft deleted”, that means that the group names that we’ve used by Microsoft Teams Team, are not deleted and their respective sites. Usually, I use a name convention to my Teams saying testing some like this – testingCLIENT001.

Using this name convention is import so you only clear the correct Microsoft Teams Team. The PowerShell below deleted those resources that we’ve put on the recycle bin.

Note: Make sure you’ve installed the PowerShell modules required

$Name = "testing"
Connect-AzureAD
Get-AzureADMSDeletedGroup | ForEach-Object { 
    If ($_.Url -cmatch $Name ) {
        Write-Output ("Site will be deleted {0}" -f $_.Url)  
        Remove-AzureADMSDeletedDirectoryObject -Id $_.Id 
    }
}
Connect-PnPOnline -url https://Contoso-admin.sharepoint.com/ -UseWebLogin
Get-PnPTenantRecycleBinItem | ForEach-Object {  
    If ($_.Url -cmatch $Name) {
        Write-Output ("Site will be deleted {0}" -f $_.Url)  
        Clear-PnPTenantRecycleBinItem  -Url $_.Url -Force
    }
}

When this script is concluded, you should have all the SharePoint sites and Microsoft Groups deleted and no longer have testing sites everywhere.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *