Deleting multiple records

Rahul Kathuria
Tera Expert

Hi Everyone,

I am trying to delete around 8k records in our instance. Below script is not working. Do we really require while loop in order for the below script to work properly?

var qry = 'short_description=Warning: could not send message for past 4 hours';

var psc = new GlideRecord('hr');

psc.addEncodedQuery(qry);

psc.setLimit('10');

psc.query();

gs.print("the count is "+psc.getRowCount());

psc.deleteMutiple();

Thanks,

Rahul kathuria

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Rahul,



Here is the modified business rule along with comment. Make sure table name is correct


updateHrRecords();


function updateHrRecords()


{


var psc = new GlideRecord('hr'); //Make sure table name is correct


psc.addQuery('short_description=Warning: could not send message for past 4 hours');


psc.setLimit('10');


psc.setWorkflow(false); //Turn off business rule or notifications


gs.print("the count is "+psc.getRowCount());


psc.deleteMultiple();


}


References:


Remove All Data from a Table - ServiceNow Wiki


GlideRecord - ServiceNow Wiki


View solution in original post

9 REPLIES 9

Rahul Kathuria
Tera Expert

Hi Pradeep/Abhinay,



This is simply not working. Although i am able to delete records using below code but the processing is very slow, i mean in 140 sec it deleted only 7 records. so i am trying to search for an alternative which can do this processing without any loop or delay.



var qry = 'short_description=Warning: could not send message for past 4 hours';


var psc = new GlideRecord('hr');


psc.addEncodedQuery(qry);


psc.query();


gs.print("the count is "+psc.getRowCount());



//gs.deleteMutiple();


while(psc.next()){


        psc.deleteMultiple();


  }


Did you try using the modified query.


i ran your script in background scripts,


  1. var psc = new GlideRecord('hr'); //Make sure table name is correct  
  2. psc.addQuery('short_description=Warning: could not send message for past 4 hours');  
  3. psc.setLimit('10');  
  4. psc.setWorkflow(false); //Turn off business rule or notifications  
  5. gs.print("the count is "+psc.getRowCount());  
  6. psc.deleteMutiple();  


and this is the output:



[0:00:00.004] Script completed: script



*** Script: the count is 0

This is working now. there was a spelling mistake. i was using mutiple instead of multiple.



psc.deleteMutiple();


Glad you figured it out


Please mark response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list. Thank you