SharePoint Online: How to Delete a Term using PowerShell?
Requirement: Delete a Term from SharePoint Online Term Store.
How to Delete a Term in SharePoint Online using PowerShell?
Deleting a term is essential if you want to clean up your Term Store and ensure that all terms are organized and properly categorized. This blog post will show you how to delete a term in SharePoint Online. Please note, If you delete a term, items that had been previously tagged with the deleted term will no longer be tagged with that term! Deleting a term removes all instances of it.
Follow these steps to delete a term from Term Store in SharePoint Online.
- Navigate to your SharePoint Online admin center site. (E.g., https://<tenant>-admin.sharepoint.com)
- Expand “Content services” and click the “Term store” link on the left navigation menu.
- From the taxonomy tree view, expand the term group, Term set, and all the way through the term you would like to delete.
- Click on the little three dots in the Term Header >> Select the “Delete Term” option. Confirm the prompt to delete a term from the SharePoint Online term store.
PowerShell to delete a Term from SharePoint Online Term Store:
As a SharePoint Online administrator, you may need to delete terms from the Term Store. This article will show you how to do it using PowerShell. Remember that when deleting terms, you may also delete any associated content tagged with those terms. So be careful!
#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"
$TermName ="UAE"
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
$TermSet = $TermGroup.TermSets.GetByName($TermSetName)
#Get the term
$Term = $TermSet.Terms.GetByName($TermName)
#Delete the term
$Term.DeleteObject()
$Ctx.ExecuteQuery()
Write-host "Term '$TermName' Deleted Successfully!" -ForegroundColor Green
}
Catch {
write-host -f Red "Error Deleting Term!" $_.Exception.Message
}
Please note, this deletion is irreversible! If you delete a term, any existing documents/items with that term must be re-tagged with a different term when you edit them. So, Instead of deleting, you can “Deprecate” the term so that it will go hidden. Alternatively, you can configure any term’s “Available for tagging” property to make it unavailable on tagging.
PowerShell to Delete All Terms from a Term Set:
Let’s remove all terms from a specific term set in SharePoint Online.
#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
$TermGroups = $TermStore.Groups
$Ctx.Load($TermGroups)
$Ctx.ExecuteQuery()
$TermGroup = $TermGroups | Where-Object {$_.Name -eq $TermGroupName}
If($TermGroup -ne $NULL)
{
#Get the Term set
$TermSets = $TermGroup.TermSets
$Ctx.Load($TermSets)
$Ctx.ExecuteQuery()
$TermSet = $TermSets | Where-Object {$_.Name -eq $TermSetName}
If($TermSet -ne $NULL)
{
#Delete all Terms from the Term Set
$Terms = $TermSet.Terms
$Ctx.Load($Terms)
$Ctx.ExecuteQuery()
#Delete all Terms from the Term Set
$Terms | Foreach-object {
$_.DeleteObject()
$Ctx.ExecuteQuery()
}
Write-host "All Terms Deleted Successfully from the Term Set '$TermSetName'!" -ForegroundColor Green
}
else
{
Write-host "Term Set '$TermSetName' Exists Already!" -ForegroundColor Yellow
}
}
else
{
Write-host "Term Group '$TermGroupName' doesn't Exist!" -ForegroundColor Yellow
}
}
Catch {
write-host -f Red "Error Deleting All Terms from the Termset !" $_.Exception.Message
}
SharePoint Online: Delete Term using PnP PowerShell
Let me show you how to delete a term using the PnP PowerShell cmdlet Remove-PnPTaxonomyItem:
#Config Variables
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
#Connect to PnP Online
Connect-PnPOnline -Url $AdminCenterURL -Interactive
#Delete the Term "South America" from Term set "regions" under "Deals Pipeline" Group
Remove-PnPTaxonomyItem "Deals Pipeline|Regions|South America" -Force