SharePoint Online: Delete Term Group in Term Store using PowerShell
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. Keep in mind 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 which 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 got to delete all term sets of the group to delete the group.
PowerShell to Delete a Term Group in SharePoint Online:
If you are using SharePoint Online and need to delete a Term Group in the Term Store, you can 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 get rid of a group of terms that you no longer need.
PnP PowerShell to Delete a Term Group in SharePoint Online:
#Config Variables
$AdminCenterURL = "https://Crescent-admin.sharepoint.com"
#Connect to PnP Online
Connect-PnPOnline -Url $AdminCenterURL -Credentials (Get-Credential)
#Delete the Group "Geography"
Remove-PnPTaxonomyItem "Geography" -Force
This PowerShell script removes the group “Geography” and all its children.