Looking to Mass delete records from an instance.

scottangehr
Giga Guru

We have a new sub-prod instance that has been cloned from prod, but there is a request to removed 11 yrs of history from this instance.   I have a BG Script running but its taking an hour to delete 100 records.   I have over 2m records to remove.   Any thoughts or scripts to quickly wipe out all records on a table?   Here's what I'm running.

var rec = new GlideRecord('incident');     
rec.query();    
while (rec.next()) {        
gs.print('Inactive incident ' + rec.number + ' deleted');      
rec.deleteRecord();    
}  
1 ACCEPTED SOLUTION

scottangehr
Giga Guru

In working with ServiceNow, here is the option they suggested I try:



Navigate: Automated Test Framework > Administration > Table Cleanup


  • Click New
  • Tablename: <table to delete records>
  • Age in seconds: 1 day's worth of seconds (60x60x24 = 86400) or a week or whatever you want to keep
  • Cascade delete: true (that way it will clean up other data)
  • Then there are more conditions you can add.
  • Click Submit


Let the scheduled job Table Cleaner run it's schedule.   To check and verify there is a scheduled job.



Navigate: System Scheduler > Scheduled Jobs


  • Search Name: Table Cleaner

                  *Note the Next Action Date/Time



If the scheduled job does not exist - create a new job to repeat every hour, but this is an OOB job and should exist.



Once the scheduled job has run and completed - DELETE THE TABLE CLEANUP you created.   If not, you will continually delete the records from the table.



I was able to delete 1.8m records in about 6 hours.


View solution in original post

11 REPLIES 11

Nia McCash
Mega Sage
Mega Sage

Have you tried: Delete all records from a table   ?


I did and it times out deleting only 9 records at a time


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Scott,



One of the suggestions I have is to delete records in chunks with the method setLimit. Please refer section 10.5 for more info.


GlideRecord - ServiceNow Wiki


Here is the best practice.


When you prepare to delete a large number of records from a table, consider the following guidelines to minimize the impact on performance.


  • Limit the number of records to be deleted in a single delete action to prevent the table from being locked. Use the setLimit() method described at setLimit.
  • Minimize triggering an excessive amount of business rules as a result of this deletion. Use the setWorkflow(boolean e) method described at setWorkflow.


Modified code


deleteIncRec();


function deleteIncRec()


{


var rec = new GlideRecord('incident');


rec.setLimit(1000);


rec.query();


while (rec.next()) {


  //gs.print('Inactive incident ' + rec.number + ' deleted');


  rec.setWorkflow(false);


  rec.deleteRecord();


}


}