Background scripts for Deletion

Jyoti36
Mega Expert

Hi,

I need to write background script to delete some unwanted data from multiple tables. I am just concerned about following the best approach like performing deletions from Master table and then the child tables. I am thinking to write a scheduled job or may be an on-demand script for the same.
Can anyone please share some scripts or guide on how can I handle the data correctly as this will involve multiple table records.

Regards
Jyo

1 ACCEPTED SOLUTION

Hi Jyo,



For multiple tables, just wrap your deletion script in a loop. Something like this. WARNING: UNTESTED CODE AHEAD!



(function () {


      var tableList = ['cmdb_ci', 'task', 'cmn_location', 'cmn_department' ];


      for (var i = 0; i < tableList.length; i++) {


                  var gr = new GlideRecord(tableName[i]);


                  gr.query();


                  gr.deleteMultiple()


      }


})();



As far as base tables and extended tables... deleting records on a base table (like cmdb_ci or task) deletes all child records as well - just tested.)


View solution in original post

16 REPLIES 16

You can add multiple tables in one script. Looks like this:



doit("problem_task");


doit("problem");


etc



function doit(table) {


var gr = new GlideRecord(table);


gr.query;


gs.log('(' + table + ') ' + gs.getRowCount());


gr.deleteMultiple();


}


Thanks Michael I will try your solution for sure!


Once again thanks for your time.



Regards


Jyo