SharePoint Online: Delete Term Set using PowerShell

Requirement: Delete a Term set in SharePoint Online

How to Delete a Term Set in SharePoint Online?
To delete a term set from 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 on "Term store" link on the left navigation menu.
  • Now, from the term store, You can navigate to the term set that you want to delete and click on "Delete term set" from its context menu.
  • Confirm the "Delete term set" popup by clicking on "Delete" button. This deletes all terms under the deleted term set, any of the terms in use moved to "Orphaned terms" under "System" term group.
    how to Delete Term Set in SharePoint Online
You can also use the classic Term store from: More Features >> Term store in the left navigation of 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 "Delete Term Set" option. 
    SharePoint Online Delete Term Set using PowerShell
  • Confirm the prompt once to delete the term set from 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 more simpler! Just provide the term set path for Remove-PnPTaxonomyItem cmdlet.
#Config Variable
$AdminCenterURL = "https://crescenttech-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 "deals pipeline" group
sharepoint online pnp powershell to delete termset from termstore

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.