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

danpatino
Tera Expert

Priya,

 

 

 

I've used a similar query to delete 50k+ records from a table before (although my encoded query looked different).   The delete just takes a while because it cascade deletes. I think you'll have to be patient with it.   There are other solutions like gs.sql('TRUNCATE TABLE .... but that can leave you with a much bigger mess than what you have.   This article explains in further detail.

 

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


tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Priya,



Probably when you thought the query was "hanging" it was busy deleting records.


Have you checked with getRowCount(); to see how many rows are still awaiting deletion?


Maybe you have actually succeeded?



Best Regards



Tony



bernyalvarado
Mega Sage

Hi Priya,



I would suggest you try to delete the records using an on demand schedule job. That way the process would not be associated to a UI session and would run in the background without interruptions.



Thanks,


Berny