Delete taxonomies

The Sitefinity CMS API enables you to delete a taxonomy. The following sample demonstrates how a taxonomy and all its children (taxa) can be deleted:

C#
using System.Linq;
using Telerik.Sitefinity.Taxonomies;
using Telerik.Sitefinity.Taxonomies.Model;

namespace SitefinityWebApp
{
    public class DeleteTaxonomiesOperations
    {
        /// <summary>
        /// Deletes a taxonomy, including all of its taxa.
        /// </summary>
        /// <typeparam name="TTaxonomy">The type of taxonomy - <see cref="FlatTaxonomy"/> or <see cref="HierarchicalTaxonomy"/>.</typeparam>
        /// <param name="partialTitle">Part of the taxonomy title.</param>
        public void DeleteTaxonomy<TTaxonomy>(string partialTitle)
            where TTaxonomy : class, ITaxonomy
        {
            var manager = TaxonomyManager.GetManager();
            var taxonomy = manager.GetTaxonomies<TTaxonomy>().SingleOrDefault(t => t.Title.Contains(partialTitle));
            if (taxonomy != null)
            {
                foreach (var taxon in taxonomy.Taxa)
                {
                    manager.Delete(taxon);
                }

                manager.Delete(taxonomy);
            }
            manager.SaveChanges();
        }
    }
}

The method accepts a generic TTaxonomy type to support deleting both hierarchical and flat taxonomies.You must pass the taxonomy type (such as FlatTaxonomy or HierarchicalTaxonomy), as well as the title of the taxonomy you wish to delete.

NOTE: Using this method you will delete both the taxonomy and its children.

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
New to Sitefinity?