- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2017 01:12 PM
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();
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2017 06:40 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2017 01:16 PM
Have you tried: Delete all records from a table ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2017 04:13 AM
I did and it times out deleting only 9 records at a time

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2017 01:18 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2017 01:20 PM
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();
}
}