How to resolve the background scripts issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2025 06:59 AM - edited ‎02-19-2025 06:59 AM
Hi,
I have 5 lakhs+ records in sc_req_item table so we have used background script to delete the records and close the background scripts tab but still it is running i want to stop the backgroung script because based on the below script it is deleting 50 records instead of 10000 records.
Can anyone please help on this, It will be useful.
var batchSize = 10000; // Process 10,000 records at a time
var totalDeleted = 0;
var gr;
do {
gr = new GlideRecord('sc_req_item');
gr.setLimit(batchSize);
gr.query();
var count = 0;
while (gr.next()) {
gr.deleteRecord();
count++;
}
totalDeleted += count;
gs.sleep(1000); // Prevent system overload
} while (count > 0);
gs.print("Deleted " + totalDeleted + " records from sc_req_item.");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-11-2025 12:48 AM
Hi,
Go to Active Transactions there you find your script and try to kill it your BG script then it will stop.
Here do some changes on your script.
var batchSize = 10000;
var totalDeleted = 0;
var gr;
var condition = true; // Control loop execution
while (condition) {
gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_created_on', '<', gs.daysAgo(180)); // Delete records older than 180 days
gr.setLimit(batchSize);
gr.autoSysFields(false); // Prevent unnecessary updates to sys fields
gr.query();
var count = gr.getRowCount(); // Get count of records in current batch
if (count > 0) {
gr.deleteMultiple(); // Bulk delete operation
totalDeleted += count;
gs.info("Deleted " + count + " records in this batch...");
gs.sleep(1000); // Prevent system overload
} else {
condition = false; // Stop when no more records exist
}
}
gs.info("Total Deleted Records from sc_req_item: " + totalDeleted);
If my response helped you, please accept the solution and mark it as helpful.
Thank You!