Delete multiple records using background scripts

Lucien1
Giga Expert

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

1 ACCEPTED SOLUTION

Lucien1
Giga Expert

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.


View solution in original post

15 REPLIES 15

Chuck Tomasi
Tera Patron

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'));




Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

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();  


}  


Lucien1
Giga Expert

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.


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
}

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();