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

Michael Fry1
Kilo Patron

Hi Michael,



Thanks for your quick reply!


Actually, I am going to wipe down data from multiple tables in one script with some queries. So just concerned what's the best way. Now I am trying to write a Fix script.


Any thoughts?



Regards


Jyo


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.)


Thanks chuck, that' what I was looking for.



Also one more thing, like If I want to run the same query then this will work as it is...something like this...


(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.addEncodedQuery(u_owner=abf6b5030f5d9699f5a07f5ce1050eda^sys_created_by!=Jyo);  


gr.query();  


gr.deleteMultiple()  


}  


)();