Knowledge Base

Working With Custom Hooks: OnCategoryDeleted

The taxonomy system is a major contributing factor to the functionality of content on a web site, and deleting categories can cause unforeseen issues. We examine how to use this custom hook to recover category data that has been accidentally deleted.

The taxonomy system and its categories are often a major contributing factor to the functionality of content on a web site. They are built to categorize, query, filter, and sort data on pages.  

Taxonomy is often used for simple things like filtering content in search results by type or displaying a list of employees based on their department. But it can also be used to support more complex functionality, like serving targeted content to site visitors based on unique identifiers (e.g., location or cookie value).  

The CMS taxonomy system, where categories are managed (created, moved, edited, and deleted) does not have a recycle folder. Categories that are deleted are immediately removed from the system, along with all their descendent categories. Deleted categories are also removed from any associated Content Item (page, component, asset, etc.).  

Once deleted, the action cannot be undone. The only option for recovery lies in server backups of the taxonomy system. Changes to categories are published to a target sitewide immediately on the next publish, whether incremental or full. 

This high touch and possible high impact system often inspires developers to use this hook to limit which categories can be deleted, which can be very useful, especially when protecting the categories with many descendants. It might also be used to keep systems in sync when a category is deleted, triggering the archival of data that references the deleted category. 

Users can trigger this hook when they take the following manual actions in the CMS: 

  • CMS Version 10.6: Right-click on the taxonomy category, select Delete from the Context Menu, and confirm in the resulting warning dialog. 
  • CMS Version 10.5 and Before: Hover over a taxonomy category, click the minus (-) icon that appears, and confirm in the resulting warning dialog. 


The custom hook will trigger only on the taxonomy category that was directly deleted (it will not trigger on any descendant categories).  

If you are looking to perform actions on all deleted categories, you may need to manually gather all descendants of the deleted category and loop through them individually. 


Export a list of Categorized Content Items as CSV for Backup 

Since the taxonomy system in the CMS does not have a recycle bin to act as a buffer for deleted categories, this script archives the associations from a deleted category into a CSV file that could be used to recreate the data if a mistake was made in the deletion. The script creates a unique file, which is named and dated for the specific deletion. 

First, gather the following objects and values:  

  • Name of the deleted category 
  • Category ID 
  • List of content items associated with the category 

Put together the category name, ID, and the current date to create the file name of the new CSV. 

The CreateAsset method is used to make a new asset in the Asset Management System where your CSV will be stored so that you, or your users, can easily access it when needed.

Gather the necessary objects to build the method: The asset folder in the Asset Management System where your CSV will live once it's created and the asset schema from the Schema Manager, which is the container in which your CSV will be stored. Make sure to select the correct type of asset schema for your CSV. If you do not know the right asset schema to use, you can find that by going to Administration > Schema Designer > Asset Schemas and locating the schema that you use to store CSV content. 

Finally, create the asset with the file. 

string categoryName = category.Name; 

string categoryId = category.Id.Replace("/","-"); 

Dictionary<string, string> categoryPage = category.ContentItems(); 

string filePath = "Deleted-"+categoryName+ "-" + categoryId + "-" + DateTime.Today.ToString("yyyy-MM-dd") + ".csv"; 


var assetfolder = session.Site.AssetFolder("af/21"); 

IAssetSchema csvSchema = session.SchemasManager.AssetSchema(AssetType.Text); 

StringBuilder sb = new StringBuilder(); 

sb.AppendLine("Content Name, Content ID"); 

foreach (var cat in categoryPage) 


sb.AppendLine(cat.Value + "," + cat.Key); 



session.Site.CreateAsset(assetfolder, csvSchema, filePath, false, new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()))); 

Possible Enhancements 

You might consider enhancing this script in the following ways: 

  • Currently the asset folder is hard-coded. This could cause issues if the folder gets deleted. Consider other options such as looking for the folder by name and creating it if it doesn’t exist. In the least, verify that it exists before proceeding. 
  • Consider assigning the new asset to a group that is meant to monitor these actions and who has the ability to recover deleted categories. 
  • The script currently only works for a single category being deleted. You will have to account for instances where a user deletes a category with descendants. 
  • Deletions usually don’t occur often, but you might consider archival processes if the folder gets too full. 
  • RELEASE: 10.x
  • Published: March 30, 2023
  • LAST UPDATED: April 14, 2023
  • Comments: 0

Please login to comment


There are no comments yet.