- 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 05:33 PM
Check out doit Remove All Data from a Table - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2016 06:40 PM
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

- 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:07 PM
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()
}
)();