This is a cross-post from rjesh.com
Lately, I have been working on creating a Teams template using Graph APIs and PnP Tenant template. Quickly, I accumulated a lot of Teams and decided to do a clean-up. I have achieved it by combining Office 365 CLI with PowerShell Core. My goal is to delete only the specific teams and not everything in my tenant, and for all my test teams I have used a naming convention which starts with 'RJ'.
## Get teams to delete
$teams = o365 teams team list -o json `
--query '[?starts_with(displayName, `RJ`) == `true`].id' `
| ConvertFrom-Json
foreach ($team in $teams) {
Write-Host "Deleting..." $team -ForegroundColor Green
o365 teams team remove -i $team --confirm
}
Above is a very simple script (✌🏼yay it works in Mac, PC and even in Cloud Shell ), the key thing to notice here is @office365cli support for --query parameter which uses JMESPath. In the query parameter, I have used starts_with function to get only my test Teams which starts with the display name 'RJ' and used team remove command to get rid of them.
You can change the query parameter as required to get the filtered result. For example, if you want to get all Teams except "Mark 8 Project Team"
o365 teams team list -o json --query '[?displayName != `Mark8 Project Team`]'
Note Just like me If you are wondering, even after deleting the Teams it still shows up in the Teams App then you should read this - techcommunity post which details about the latency of few hours required.
If you want to delete the Office 365 groups then use the below script
## Get groups to delete
$groups = o365 aad o365group list -o json `
--query '[?starts_with(displayName, `SET`) == `true`].id' `
| ConvertFrom-Json
foreach ($group in $groups) {
Write-Host "Deleting..." $group -ForegroundColor Green
o365 aad o365group remove -i $group --confirm
}
Photo by Philipp Katzenberger on Unsplash.
Top comments (0)