How to delete multiple records using scheduled jobs

ramyashree
Giga Contributor

Hi All,

I need to delete certain records in a table where a particular field has no value.

I have done it for single record using the below code.

var gr = new GlideRecord('table_name');

gr.addQuery('sys_id','07dbd0ea4f005200634df5a18110c75c');

gr.query();

gr.next();

gr.deleteRecord();

How can i fetch multiple records and delete them using Scheduled jobs.

Thanks,

Ramya

1 ACCEPTED SOLUTION

srinivasthelu
Tera Guru

var gr = new GlideRecord('table_name');


gr.addNullQuery('fieldName');


gr.deleteMultiple();


View solution in original post

7 REPLIES 7

vant
Tera Expert

You can use deleteMultiple() instead of doing it one by one. Just make sure the query returns all the records that need to be deleted, and then execute this command. Look at this wiki for an example.



GlideRecord - ServiceNow Wiki


ramyashree
Giga Contributor

Thanks Van for your help.


zica
Giga Guru

Ramya,



If you are deleting multiple records then the 'deleteMultiple' method can be used as a shortcut,


Here is a sample to delete all inactive incident :



//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



if you would like to use a GlideRecord, here is a sample of script which deletes inactive incident (as above) :


var gr = new GlideRecord('incident');


gr.addQuery('active',false);


gr.query();


while (gr.next()) {


    //Delete each record in the query result set


    gr.deleteRecord();


}




you can addQuery('fielname', '') to get specific field with no value and also delete them.



let me know if you have any concerns



Kind regards,


ZA


Do not feel shy to mark correct or helpful answer if it helps or is correct



ramyashree
Giga Contributor

Thank you Zic.