
If you’ve ever tried to delete a Group in a Managed Metadata term store that has child term sets underneath it, you’ll likely recall seeing this error:
The group has one or more term sets and can not be deleted. Move or delete all term sets from the group before deleting the Group.
This error is a result of SharePoint protecting your data, as it won’t allow a group to be deleted while there are children term sets underneath it. (Interestingly enough, term sets can be deleted even if they have child terms.) Through the UI, this is simple to work around by individually deleting each term set underneath the group, and then finally deleting the group itself. However, if you’re trying to do this when there are several term sets underneath the group, or perhaps you have several groups you need to delete which all have several term sets, PowerShell can aid you in your quest to quickly remove the children term sets and their parent group(s).
I have written a PowerShell function that you can pass one or more groups to, and it will delete all of the child term sets, as well as the group. If you do not call $termStore.CommitAll(), however, your change will not be committed. Purposely removing the commit line is a good way of running a -WhatIf scenario with the function where the changes are not actually committed.
PowerShell Function
function DeleteGroupAndChildrenTermSets { param($MMGroups = $(Throw "You must provide one or more Groups to delete")) if ($MMGroups -ne $null) { $MMGroups | % { Write-Host "Deleting Term Sets for Group: " $_.Name; $_.TermSets | % { $_.Delete(); Write-Host "--Deleted TermSet: " $_.Name; } } $MMGroups | % { $_.Delete(); Write-Host "Successfully Deleted Group: " $_.Name -Fore Green } } else { Write-Host "No groups provided for deletion" } }
Example Usage
$taxSvc = Get-SPTaxonomySession -Site http://devserver $termStore = $taxSvc.DefaultSiteCollectionTermStore $mmGroups = $termStore.Groups | ? { $_.Name -eq "MyGroup" } DeleteGroupAndChildrenTermSets -MMGroups $mmGroups $termStore.CommitAll(); #Comment out to not commit change
I hope that you’ve found this function useful.
Cheers,
Matt