ServiceNow Scripting – Bulk delete records with status

ServiceNow Scripting will be a bit of a series of mini-scripts, tools, one-liners, etc that I often use to assist in projects. This post outlines the task of performing a bulk delete of records.

There are many ways you may consider deleting records from a table, each with their Pros and Cons:

  1. Create a ServiceNow HI support request
  2. Use the Table configuration page’s “Delete All Records” through the UI
  3. Use a Background Script/Fix Script
  4. Use a SQL statement to truncate the table

I prefer option #3 myself almost always. #1 is attractive because the action comes with a bit of a “warranty”, but the turnaround time may be longer. For #2, the UI can have weird hiccups in my opinion, and I can’t see just exactly what it’s doing in the background (or progress). #4… well, let’s just say that if you’re considering doing things that way, I doubt you’re reading this article.

So we’re scripting. First I’d advise to do this in a Fix Script rather than a Background Script. This will allow you to run the delete process without tying up your user’s session.

There are a few other things to note when deleting records through a script – the delete method types:

  1. gr.deleteRecord();
  2. gr.deleteMultiple();

deleteMultiple() is a very appealing option. It deletes everything at once, so why not do it? One thing to know about ServiceNow methods is that they are just a fancy layer of code on top of normal Javascript. The performance of deleteMultiple is slower than deleteRecord due to how it loops, and it also ignores workflows.

Which brings us to our last point – gr.setworkflow(‘false’); This is something that’s going to drastically increase the performance of your queries, but at the cost of possibly not deleting all related records. Setting the workflows off will disable cascading Business Rules.

Below is my default “clear out a bunch of records” script. Use as you’d like!

/*
// ===================================================================
// Purpose:     Delete all records from a table
// Use case:    Clear out records in the Software Package and Software Install table
// Variables:   tableName ; runWorkflows
//              ---------------------------
//                  tableName = table to delete records from
//                  encodedQuery = (optional) Query for delete filter
//                  logRate = drop a log file update every X number of record deletes
// Author:      Jordan Hladish
// ===================================================================
*/


clearTableRecords();

function clearTableRecords() {
    var tableName = 'cmdb_ci_spkg';
    var encodedQuery = 'sys_class_name!=cmdb_ci^sys_class_name!=cmdb_ci_service_group';
    var delCount = 0;
    var totalCount = 0;
    var logPaging = 500;
    
    var dropRecords = new GlideRecord(tableName);
    //dropRecords.addEncodedQuery(encodedQuery);
    dropRecords.query();
    
    gs.print(dropRecords.getRowCount());
    totalCount = dropRecords.getRowCount();
    
    
    while(dropRecords.next()){
        //dropRecords.setWorkflow(false);
        dropRecords.deleteRecord();
        delCount++;
        if (delCount % logPaging === 0){
            logCleanerUpdate("Clearing " + tableName + " - deleting " + delCount + " of " + totalCount, tableName);
        }
    }
    

    function logCleanerUpdate(msg, table) {
        gs.log(msg, "clearTableRecords Fix Script - " + table);
    }
}

You may also like

Leave a Reply

Your email address will not be published.