SharePoint Online: Delete Term Group using PowerShell
Taxonomy is key for organizing information architecture in SharePoint Online. Term groups allow you to create a structured hierarchy for metadata tagging and faceting content. But over time, as your information architecture evolves, you may need to consolidate or delete outdated term groups. Deleting outdated term groups in SharePoint Online through the user interface can be tricky, though.
While you can delete individual terms, SharePoint does not provide a built-in option to entirely delete a group. Luckily, with the PnP PowerShell, admins can easily connect to the term store and delete term groups seamlessly. In this post, I’ll walk through the steps for identifying term groups and deleting them with PowerShell in your SharePoint Online environment’s taxonomy structure.
How to Delete a Group in SharePoint Online Term Store?
If you need to delete a term group in the Term Store, it’s pretty easy to do and can be accomplished using either the SharePoint Admin center or PowerShell. Remember that deleting a term group will also delete all terms within that group. This article will show you how to delete a term group in SharePoint Online.
Follow these steps to delete a term group in the SharePoint Online term store.
- Login to your SharePoint admin center site. (E.g., https://yourdomain-admin.sharepoint.com) >> Click the services >> Term store link on the left navigation menu.
- From the taxonomy tree view, select the term group you want to delete. Click on the little three dots in the Term group Header >> Click on the “Delete Term Group” option.
- If the term group has any terms in it, you’ll get an error message: “The group has one or more term sets and cannot be deleted. Move or delete all term sets from the group before deleting the Group”. As the error message says, you must delete all term sets of the group to delete the group.
PowerShell to delete a Term Group in SharePoint Online:
If you need to delete a Term Group in the Term Store, you can also do it using PowerShell.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
#Variables for Processing
$AdminURL = "https://crescent-admin.sharepoint.com/"
$TermGroupName ="Deals Pipeline"
Try {
#Get Credentials to connect
$Cred = Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminURL)
$Ctx.Credentials = $Credentials
#Get the term store
$TaxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx)
$TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
$Ctx.Load($TaxonomySession)
$Ctx.Load($TermStore)
$Ctx.ExecuteQuery()
#Check if the given group exists
$TermGroups = $TermStore.Groups
$Ctx.Load($TermGroups)
$Ctx.ExecuteQuery()
$TermGroup = $TermGroups | Where-Object {$_.Name -eq $TermGroupName}
If($TermGroup -ne $NULL)
{
#Delete all Term sets in the Term group
$TermSets = $TermGroup.TermSets
$Ctx.Load($TermSets)
$Ctx.ExecuteQuery()
$TermSets | Foreach-object {
$_.DeleteObject()
$Ctx.ExecuteQuery()
}
$TermStore.CommitAll()
#Delete the Term Group
$TermGroup.DeleteObject()
$Ctx.ExecuteQuery()
Write-host "Term Group '$TermGroupName' Deleted Successfully!" -ForegroundColor Green
}
else
{
Write-host "Term Group '$TermGroupName' Doesn't Exist!" -ForegroundColor Yellow
}
}
Catch {
write-host -f Red "Error Deleting Term Group!" $_.Exception.Message
}
This can be useful if you want to eliminate a group of terms you no longer need.
PnP PowerShell to Delete a Term Group in SharePoint Online:
Use the PnP PowerShell cmdlet Remove-PnPTaxonomyItem to remove an unwanted term group in SharePoint Online.
#Config Variables
$AdminCenterURL = "https://Crescent-admin.sharepoint.com"
#Connect to PnP Online
Connect-PnPOnline -Url $AdminCenterURL -Interactive
#Delete the Term Group "Sectors"
Remove-PnPTaxonomyItem "Sectors" -Force
This PowerShell script removes the group “Sectors” and all its children.
You can also use the Remove-PnPTermGroup cmdlet to remove a term group and all its term sets.
Remove-PnPTermGroup -Identity "Corporate"
Remove-PnPTermGroup -Identity 23f11c8e-8276-4f60-b1d5-3c0e92cb435c