Skip to main content

ServiceNow truncating very large tables which are no longer used - and example

Issue:
Request regarding the deletion of records on the following tables
- azure_billing_daily
- azure_billing_monthly

Business Impact:
Tables have not been used since 2018

Solution Proposed:
This is a common question regarding data management. We have an article which provides several methods to handle the data and possible deletions:

https://support.servicenow.com/kb?id=kb_article_view&sys_kb_id=fbc46dd2db266010c9c302d5ca9619a5



The article covers the following four topics:
UI actions
Clone exclude rules
Table cleanup policies
JavaScript

We recommend testing in subproduction environment(s) prior to any implementation in production. ServiceNow does not intervene in data removal requests unless the presence of those records are causing a business critical operation to be unusable.

Next Steps:
Would you be able to review the cleanup methods mentioned and let us know if you have any additional questions. We would be happy to follow up.

 

 =================

example script - tables no longer used:

var grBusRule = new GlideRecord('sys_script');
grBusRule.addActiveQuery();
grBusRule.addQuery('name', 'custom-Azure Billing Query Based On Role');
grBusRule.query();
if (grBusRule.next()) {
    gs.log('PLEASE DISABLE BUSINESS RULE "custom-Azure Billing Query Based On Roles" BEFORE PROCEEDING', 'fixScr_16482');
    
} else {

    var stable1 = "azure_billing_daily"; //--4M +records
    var stable2 = "cloud_mgmt_billing_daily"; //--2.5M +records
    var stable3 = "cloud_mgmt_tag_history_map"; //--874K +records
    var stable4 = "cloud_mgmt_rsrc_tag_history"; //--226K+ records
    var stable5 = "azure_billing_monthly"; //--156K +records


    truncateAzureTable(stable1); //--timing: took approx 40 mins for the largest table
    /*truncateAzureTable(stable2);
    truncateAzureTable(stable3);
    truncateAzureTable(stable4);
    truncateAzureTable(stable5);*/

}

function truncateAzureTable(tableName) {
    var bTruncate = false;
    bTruncate = true;

    var Tgr = new GlideRecord(tableName);
    Tgr.query();
    if (bTruncate) {
        gs.log(tableName + ':: table truncate START', 'fixScr_16482');
        /*skip business rules*/
        Tgr.autoSysFields(false); //--leave last updated intact
        Tgr.setWorkflow(false); //--skip business rules and notifications
        Tgr.deleteMultiple();
        gs.log(tableName + ':: table truncate END', 'fixScr_16482');

    } else {
        gs.print(tableName + '--TABLE ROW COUNT: ' + Tgr.getRowCount());

    }
}

(ServiceNow )

Comments

Popular posts from this blog

Get URL Parameter - server side script (portal or classic UI)

Classic UI : var sURL_editparam = gs . action . getGlideURI (). getMap (). get ( ' sysparm_aparameter ' ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); } Portal : var sURL_editparam = $sp . getParameter ( " sysparm_aparameter " ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); }

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){