CI mass deletion using background script

jiral
Giga Sage

We have about a number of duplicate CIs which we set on "Archived" status to know that they are duplicate and we want to delete them. Using a background script I want to delete these duplicate CIs meeting the following conditions:

1. Status is Archived

2. Class is either Network Gear OR IP Switch OR Router

3. In cmbd_ci table

  1. var duplicate = new GlideRecord('cmdb_ci');  
  2. duplicate.addQuery('install_status','=', '200');
  3. duplicate.addQuery(('sys_class_name','=', 'cmdb_ip_switch')||('sys_class_name','=','cmdb_ci_ip_router')||('sys_class_name','=', 'cmdb_ci_netgear'));
  4. duplicate.query();  
  5. while (duplicate.next()) {    
  6. gs.log('Deleted Duplicate CIs ' + duplicate.number + ' deleted');  
  7. duplicate.deleteRecord();  
  8. }  

Can you inspect the script above for issues before I run this against the cmdb_ci table?

1 ACCEPTED SOLUTION

You'll get a bit better performance with this:



var duplicate = new GlideRecord('cmdb_ci_netgear');    


duplicate.addQuery('install_status','=', '200');    


duplicate.deleteMultiple();    



GlideRecord - ServiceNow Wiki


View solution in original post

7 REPLIES 7

peterh_smith
Tera Contributor

I hope you caught the duplicates before relationships were attached to them, and marked the duplicate that had no relationships.   If all of your relationships are built by discovery, they'll come back.   But if you have manually created relationships, you may need to be careful.


Thanks Chuck and Peter. I ran the script Chuck provided and monitored if those CIs will come back. It has been over 2 weeks and these CIs did not come back.


Hi jeffiral,
I am looking to delete a huge amount of data, which is manually entered could you please suggest me a method to do it?
Thanks