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

Yes. You can use the code inside a fix script. Fix scripts are a great way to move things from one system to another. I was just updating one of mine.



Fix scripts have the benefit of running automatically when an app is installed (e.g. from source control or the publish/install process).



Fix Scripts - ServiceNow Wiki



Scheduled jobs are another way. They can run on a timed basis or on demand (with the Execute Now button.)



Creating a Scheduled Job - ServiceNow Wiki


Actually, I have never used the as such.


And based on your yesterday's code I have created four similar scripts like below as their queries are different :


==================================================================


(function () {  
var tableList = ['wm_xxxxxx', 'u_service_xxxxx', 'u_wm_xxxxx, 'u_references'];  
for (var i = 0; i < tableList.length; i++) {  
var gr = new GlideRecord(tableName[i]);
gr.addEncodedQuery('sys_domainSTARTSWITHXX^ORsys_domainSTARTSWITHXX');    
gr.query();  
gr.deleteMultiple()  
}  
})();


==================================================================



So, I have basically 3 more similar scripts. Do you recommend using this as fix scripts will be a best approach? I reckon, 'Run Script' is bit a challenging   for deletions.


Sorry , If I am bothering you... but I am really concerned about the data.



Regards


Jyo


Hi Jyo,



You are right to be cautious when it comes to deleting data. Best practice would suggest to make a backup (export the data from those tables to XML before the deletion.)



Fix scripts are nice because they can run automatically one time. Some people don't like that level of automation - especially when it comes to deleting records and choose a scheduled job.



Also note - unless you are using a scoped application (managed from Studio) the scheduled job is not captured by an update set automatically.


Yup great!


So, I will go ahead and copy paste the script in FIX Scripts and get my job done.


Thank you so much once gain! You are awesome!



Regards


Jyo


You are very welcome.