SharePoint Online: Delete a Term Set using PowerShell
Requirement: Delete a Term set in SharePoint Online.
How to Delete a Term Set in SharePoint Online?
In SharePoint Online, you can delete a term set that is no longer needed. Deleting a term set can help you keep your term store organized and reduce clutter. In this article, we will discuss how to delete a term set in SharePoint Online.
To delete a term set from the SharePoint Online term store, follow these steps:
- Login to your SharePoint Online admin center site. (E.g., https://yourdomain-admin.sharepoint.com)
- Expand “Content services” and click the “Term store” link on the left navigation menu.
- Now, from the term store, You can navigate to the term set you want to delete and click “Delete term set” from its context menu.
- Confirm the “Delete term set” popup by clicking the “Delete” button. This deletes all terms under the deleted term set, and any terms in use are moved to “Orphaned terms” under the “System” term group.
After deleting the term set, you can verify that it has been deleted by refreshing the Term Store Management Tool page and checking that the term set is no longer listed. You can also go back to the group that contained the term set and confirm that the term set has been deleted.
You can also use the classic Term store from: More Features >> Term store in the left navigation of the SharePoint Admin center. (URL shortcut: https://tenant-admin.sharepoint.com/_layouts/15/TermStoreManager.aspx)
- From the taxonomy tree view, Expand and select the term set you want to delete. Click on the little arrow in the Term set >> Select the “Delete Term Set” option.Â
- Confirm the prompt once to delete the term set from the SharePoint Online term store.
PowerShell to Delete Term Set in SharePoint Online
We can also remove a term set programmatically 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 ="Regions"
$TermsetName="MENA"
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()
#Get the term group
$TermGroup=$TermStore.Groups.GetByName($TermGroupName)
#Get the term set to delete
$TermSet = $TermGroup.TermSets.GetByName($TermsetName)
#Delete the term set
$TermSet.DeleteObject()
$Ctx.ExecuteQuery()
Write-host "Term Set '$TermSetName' Deleted Successfully!" -ForegroundColor Green
}
Catch {
write-host -f Red "Error Deleting Term Set!" $_.Exception.Message
}
Delete All Term Sets from a Term Group in SharePoint Online:
How about deleting all term sets from a term group?
#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 ="Regions"
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()
#Get the term group
$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()
#Delete all Term Sets from the Group
$TermSets | Foreach-object {
$_.DeleteObject()
$Ctx.ExecuteQuery()
}
Write-host "All Term Sets Deleted Successfully from the Term Group '$TermGroupName'!" -ForegroundColor Green
}
else
{
Write-host "Term Group '$TermGroupName' Doesn't Exist!" -ForegroundColor Yellow
}
}
Catch {
write-host -f Red "Error Deleting Term Sets!" $_.Exception.Message
}
PnP PowerShell to Delete a Term Set in SharePoint Online:
PnP PowerShell makes it much simpler! Just provide the term set path for the Remove-PnPTaxonomyItem cmdlet.
#Config Variable
$AdminCenterURL = "https://Crescent-admin.sharepoint.com"
#Connect to PnP Online
Connect-PnPOnline -Url $AdminCenterURL -Credentials (Get-Credential)
#Delete the Term set "regions" under "Deals Pipeline" Group
Remove-PnPTaxonomyItem "Deals Pipeline|Region" -Force
This removes the “Region” term set from the “deals pipeline” group.
Please note, deleting a term set will also delete all the terms within that term set. If you want to keep the terms, you should consider moving them to a different term set. In this article, we explained how to delete term sets that are no longer needed, which will help you keep your term store organized.
How to restore a deleted term set?