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?
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)
  • 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 arrow in the Term Header >> Select "Delete Term" option. Confirm the prompt to delete a term from SharePoint online term store.
    SharePoint Online Delete  Term using PowerShell

PowerShell to delete a Term from SharePoint Online Term Store:
#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 retagged with a different term when you edit them. So, Instead of delete you can "Deprecate" the term so that it will go hidden. Alternatively, you can configure "Available for tagging" property of any term 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
#Config Variables
$AdminCenterURL = "https://crescent-admin.sharepoint.com"

#Connect to PnP Online
Connect-PnPOnline -Url $AdminCenterURL -Credentials (Get-Credential)

#Delete the Term "South America" from Term set "regions" under "Deals Pipeline" Group
Remove-PnPTaxonomyItem "Deals Pipeline|Regions|South America" -Force

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.