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

I have a question here what if i need to skip the records returned from the encoded query and delete the rest of the records ?

Hi, just as a heads up, you actually don't need to put a gr.____Multiple() into a loop. deleteMultiple() and updateMultiple() both operate on the query itself, so the code should look like this:

 

var gr = new GlideRecord('pa_scores');
gr.addQuery('breakdown.name','=', 'Model Category');
gr.addEncodedQuery('sys_created_on<=' + gs.dateGenerate('2016-03-31','23:59:59'));
gr.query();
gr.deleteMultiple();

 

Effectively what your version of the code does is enter the while loop, run the delete on the query, check the gr.next() which will return false because all of those records were deleted, and then exit.

Not the biggest difference, but just letting you know you can clean up a couple instructions.

Hope this helps!

james jorden
Kilo Contributor

//Find all inactive incidents and delete them all at once
var gr = new GlideRecord('incident');
gr.addQuery('active', false);
gr.deleteMultiple(); //Deletes all records in the record set according to Digital Marketing Agency requirements

Victor Escobar
Tera Contributor

Is there any way to show the results of the query before deleting?

Danmax
Tera Expert

Here is what I used to clear all records from Scope Application where we need to clear the records to import clean versions of the fields.


var gr = new GlideRecord('Table Name');

// Create a GlideDateTime object for '2023-09-04 23:59:59'
var dateThreshold = new GlideDateTime('2023-09-04 23:59:59');
gr.addQuery('sys_created_on', '>=', dateThreshold);

gr.query();

while (gr.next()) {
gs.info(gr.sys_id);
// gr.deleteRecord(); // You can uncomment this line to delete the records if needed.
}