How to delete 100 or 1000 records at a time in ServiceNow.

priyajames
Kilo Contributor

Hi friends,

I was wanting to delete some 23,387 records from the requested item table based on a certain criteria as below. I have my code as seen below but please help me with how I can tweek the code more so that I can ask it to delete a few hundred or 1000 records at a time as if i just run deleteMultiple() it hangs.

the code:

var delRec = new GlideRecord("sc_req_item");

var queryRec   = (("requestISEMPTY") && ("request.u_requested_byISEMPTY") && ("request.requested_forISEMPTY"));

delRec.addEncodedQuery(queryRec);

delRec.deleteMultiple();

Please assist.

1 ACCEPTED SOLUTION

russell_miller
Kilo Guru

you could add this just before the deleteMultiple



delRec.setLimit(1000);



To limit to a thousand... (or however many you wish)



RM


View solution in original post

6 REPLIES 6

Thansk Berny. This got me started. Exactly what I needed. You'll most likely need to tweak the particulars for your scenario (copy/paste will probably not work).



For anyone who likes setpped-out instructions like me, here's what I did:


  • Navigate to System Definition > Scheduled Jobs
  • New > "Automatically run a script of your choosing"
  • Name: [A name that you'll recognize later]
    Active: true
    Run: Once
    Starting: (blank)
    Conditional: false
    Run this script:

                  var gr = new GlideRecord('table'); //Replace 'table' with the table in which you want to delete the records


                  gr.addQuery(("state=1")&&("sys_created_on<javascript:gs.daysAgoStart(1)")); //In my case want to delete items w/ 'New' state (value of 1) that were created before yesterday (originally I only wanted to delete records created before today so the argument sent in gs.daysAgoStart() was '0' instead of '1').


                  gr.deleteMultiple(); //Delete the records that were found.



  • Save the form then click the 'Execute Now' button to run it.

You're welcome Aaron. I'm glad it was helpful!



Thanks,


Berny