- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2016 06:45 AM
Hi all,
I have modified this script I found to try delete multiple records but so far it fails.
I am going to be using the scripts background function as it is just a 1 time event. The records are before the 1st of April so I added an encoded query. Can anyone see where I am going wrong as I can't sit and delete all these records manually.
var gr = new GlideRecord('pa_scores');
gr.addQuery('breakdown','=', 'Model Category');
gr.addEncocdedQuery(sys_created_on<=javascript:gs.dateGenerate('2016-03-31','23:59:59'));
gr.query();
while (gr.next()) {
//Delete each record in the query result set
gr.deleteRecord();
}
Thanks,
Lucien
Solved! Go to Solution.
- Labels:
-
Performance Analytics
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-07-2016 03:02 AM
Thanks all for your help and with a bit of changes with your guidance I got it working. Below is what I have used.
var gr = new GlideRecord('pa_scores');
gr.addQuery('breakdown.name','=', 'Model Category'); // Pradeep Sharma update
gr.addEncodedQuery('sys_created_on<=' + gs.dateGenerate('2016-03-31','23:59:59')); // Chuck Tomasi update
gr.query();
while (gr.next()) {
gr.deleteMultiple(); // Ken and Santosh update
}
It still took time to delete these records (Dev instance 1st) but now that I know it works I can do this in production.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2016 06:51 AM
Hi Lucien,
You're already running javascript, so the 'javascript' portion of your encoded query doesn't make any sense. It also needs to be in quotes. Try this instead.
gr.addEncodedQuery('sys_created_on<=' + gs.dateGenerate('2016-03-31','23:59:59'));

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2016 07:41 AM
Hi Lucien,
Here is the final script. Let us know if you are blocked.
var gr = new GlideRecord('pa_scores');
gr.addQuery("breakdown.name=Model Category^sys_created_on<=javascript:gs.dateGenerate('2016-03-31','23:59:59')");
gr.query();
while (gr.next()) {
//Delete each record in the query result set
gr.setWorkflow(false);
gr.deleteRecord();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-07-2016 03:02 AM
Thanks all for your help and with a bit of changes with your guidance I got it working. Below is what I have used.
var gr = new GlideRecord('pa_scores');
gr.addQuery('breakdown.name','=', 'Model Category'); // Pradeep Sharma update
gr.addEncodedQuery('sys_created_on<=' + gs.dateGenerate('2016-03-31','23:59:59')); // Chuck Tomasi update
gr.query();
while (gr.next()) {
gr.deleteMultiple(); // Ken and Santosh update
}
It still took time to delete these records (Dev instance 1st) but now that I know it works I can do this in production.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-15-2019 06:23 AM
Very handy.
I'm working on importing assets from an old system and my tests resulted in thousands of records that needed to be delete. The number of time's I've gone through deleting 100 or 250 at a time.
This did exactly what I wanted with only some slight tweaks:
var gr = new GlideRecord('cmdb_ci');
gr.addEncodedQuery('sys_created_on>=' + gs.dateGenerate('2019-02-15','09:00:01')); // Chuck Tomasi update
gr.query();
while (gr.next()) {
gr.deleteMultiple(); // Ken and Santosh update
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2019 04:20 AM
Once you do the query, you don't need do to the while(). deleteMultiple() works on the query, not on each record. You only need a while() if you are using deleteRecord() - doing one at a time.
var gr = new GlideRecord('cmdb_ci');
gr.addEncodedQuery('sys_created_on>=' + gs.dateGenerate('2019-02-15','09:00:01'));
gr.query();
gr.deleteMultiple();