Delete million of records in servicenow Location Table

Ashwin Perumal1
Tera Contributor

Hi Team,

I am in a need of mass deletion of records from location table. We closely have around 6 million records which needs to be deleted. What would be the best approach to go for bulk deletion of records.

As of now we planned to delete it with fix script using batching by storing sys_id and deleting those sys_ids using delete multiple. But I would like to know any other process of deleting records without much performance impact and if anyone can share any optimized code for deleting records.

Thanks in advance

Thanks
Ashwin Perumal G

3 REPLIES 3

Aditya_hublikar
Giga Sage

Hello @Ashwin Perumal1 ,

 

Yes there is OOTB option to delete/Update data in bulk :

For bulk record deleting there is OOTB option that is Data management .

 Go to target table => filter out data which you want to delete ( apply OOTB filter functionality) =>Right click => In Context Menu => click on Data management => Delete with delete preview  => cascade preview => execute now ( from related links) => wait filtered out records will get deleted .

 

Screenshot (1153).png

Aditya_hublikar_1-1790742225218.png

 

If this helps you then mark it as helpful and accept as solution.

Regards,

Aditya

Tanushree Maiti
Tera Patron

Hi @Ashwin Perumal1 

 

Are these 6 million records currently being used? What is the business justification for deleting such a large number of foundation records?

 

Have you completed the impact assessment, considering that the Location record is referenced by other records, such as Incidents, Change Requests, etc.?  

 

Refer KB  for mass deletion of data : KB0717791 Manage and mass-delete data from a ServiceNow table 

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Tejas Adhalrao
Mega Sage

hi @Ashwin Perumal1  ,

For 6 million records, I would avoid a single delete operation. The batching approach is correct, but I would process the records in small batches and commit between batches so that one long transaction does not hold locks for too long.

Also, first check whether there are references to these Location records. Deleting 6M records can trigger Business Rules, auditing, Flow/Workflow, reference cleanup, etc., which can be much more expensive than the delete itself.

A common approach is:

var batchSize = 5000;
var gr = new GlideRecord('cmn_location');
gr.addEncodedQuery('YOUR_CONDITION');
gr.orderBy('sys_id');
gr.setLimit(batchSize);
gr.query();

while (gr.next()) {
gr.deleteRecord();
}

For very large volumes, I would run this repeatedly through a Scheduled Script Execution or controlled background process rather than keeping one Fix Script running for hours.

If the records are completely disposable and you have confirmed that you do not need the associated Business Rules/audit processing, you can also evaluate deleteMultiple(), but test carefully because deleting 6M records can have a significant impact on the instance.

Before running in production, I would test with 10K → 100K → 1M records and monitor transaction time, DB load, locks, and related table growth. Also take a backup/restore point if your environment supports it.

If this helps, please mark it as Helpful and accept the solution.
Regards,
Tejas