- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-19-2016 05:22 PM
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
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-19-2016 06:50 PM
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.)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-19-2016 07:00 PM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-19-2016 07:28 PM
Thanks Michael I will try your solution for sure!
Once again thanks for your time.
Regards
Jyo