Fix script for Model Category cleanup

SumajRajNk
Tera Contributor

Hi Team,

 

Can anyone provide me the Fix script for deleting 3 Model categories (cmdb_model_category) and Product models(cmdb_model) which are referenced using these 3 Model categories.

 

Solution to this problem are highly appreciated !

1 REPLY 1

pratikjagtap
Giga Guru

Hi @SumajRajNk ,

 

Use following fix script:

(function executeFixScript() {
// Define the names or Sys IDs of the model categories to delete
var categoryNames = ['Hardware', 'Software', 'Peripheral']; // <-- change to your actual names
 
var categoryGr = new GlideRecord('cmdb_model_category');
categoryGr.addQuery('name', 'IN', categoryNames.join(','));
categoryGr.query();
 
while (categoryGr.next()) {
var categorySysId = categoryGr.getUniqueValue();
var categoryName = categoryGr.name;
 
// Delete associated cmdb_model records
var modelGr = new GlideRecord('cmdb_model');
modelGr.addQuery('model_category', categorySysId);
modelGr.query();
 
var deletedCount = 0;
while (modelGr.next()) {
modelGr.deleteRecord();
deletedCount++;
}
 
gs.info('Deleted ' + deletedCount + ' cmdb_model records for category: ' + categoryName);
 
// Now delete the category itself
categoryGr.deleteRecord();
gs.info('Deleted model category: ' + categoryName);
}
})();
 
If you prefer to filter based on Sys IDs instead of names, just change this:
 
categoryGr.addQuery('sys_id', 'IN', sysIdList.join(','));
 

If my response helped, please hit the 👍Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,
Pratik