In one of our previous blog posts, we learned how to create a term set using PnP PowerShell, and in this post, we’ll learn how to remove the term set. If you haven’t already, go to https://www.hemantkabra.in/microsoft-sharepoint/pnp-powershell-how-to-create-term-set-in-sharepoint-online/ for more information on term sets in SharePoint Online.
Prerequisites
Before we begin, ensure that the PnP PowerShell module is already installed. If you need assistance installing the PnP PowerShell, please see this article: https://www.hemantkabra.in/microsoft-sharepoint/pnp-powershell-how-to-get-started/
The Remove-PnPTaxonomyItem cmdlet in PnP PowerShell makes it very simple to remove the term set. See examples below to remove the global and local term sets.
Example 1: Remove the Global Term Set
# SharePoint Admin Center URL
$SPAdminCenterURL = "https://contoso-admin.sharepoint.com"
# Connect to the SharePoint Admin Center using Connect-PnPOnline cmdlet
Connect-PnPOnline -Url $SPAdminCenterURL -Interactive
# Name of the Term Group
$TermGroupName = "Departments"
# Term Set Name
$TermSetName = "Technology"
# Term Set Path
$TermSetPath = "$($TermGroupName)|$($TermSetName)"
# Remove the Term Set
Remove-PnPTaxonomyItem -TermPath $TermSetPath -Force
This removes the Technology term set from the Departments term group available at tenant level.
Example 2: Remove the Local Term Set
# Site Collection URL
$SiteUrl = "https://contoso.sharepoint.com/sites/departments"
# Connect to the SharePoint Site Collection using Connect-PnPOnline cmdlet
Connect-PnPOnline -Url $SiteUrl -Interactive
# Get Site Context
$Context = Get-PnPContext
# Get taxonomy session
$TaxSession = Get-PnPTaxonomySession
# Get default site collection term store
$TermStore = $TaxSession.GetDefaultSiteCollectionTermStore()
# Get the site collection group
$TermGroup = $TermStore.GetSiteCollectionGroup($Context.Site, $true)
$Context.Load($TermGroup)
$Context.ExecuteQuery()
# Term Set Name
$TermSetName = "Topics"
# Term Set Path
$TermSetPath = "$($TermGroup.Name)|$($TermSetName)"
# Remove the Term Set
Remove-PnPTaxonomyItem -TermPath $TermSetPath -Force
This removes the Topics termset from the default site collection term group.
Few points to consider from the two examples mentioned above:
- Use of -Force parameter will skip the confirmation before removing the term set. So, do not use this if you wanted the confirmation question before deletion.
- Remove-PnPTaxonomyItem cmdlet can be used to remove term as well. You just need to pass the term path in the parameter. See below mentioned Example 3.
Example 3: Remove the Term
# SharePoint Admin Center URL
$SPAdminCenterURL = "https://contoso-admin.sharepoint.com"
# Connect to the SharePoint Admin Center using Connect-PnPOnline cmdlet
Connect-PnPOnline -Url $SPAdminCenterURL -Interactive
# Name of the Term Group
$TermGroupName = "Departments"
# Term Set Name
$TermSetName = "Technology"
# Term Name
$Term = "Microsoft 365"
# Term Set Path
$TermSetPath = "$($TermGroupName)|$($TermSetName)|$($Term)"
# Remove the Term Set
Remove-PnPTaxonomyItem -TermPath $TermSetPath -Force
This code removes the term called “Microsoft 365” available in “Technology” term set in “Departments” term group at tenant level.
I hope you enjoyed reading this blog post. Please feel free to leave a comment in case you have any feedback, suggestions, or queries.