The CreatorCon Call for Content is officially open! Get started here.

How to inactivate a custom table?

Anshika Singh
Kilo Contributor

I'm new to ServiceNow, I have don't much idea do we can active / in active custom built table in servicenow.
How to inactivate a user defined table?

And Is there any way if the inactive table can automatically get deleted after certain days?

2 REPLIES 2

Shubha_Prada
Tera Guru

Hi @Anshika Singh ,
If you no longer need a custom table, you can delete it after you delete all the records in the table.

 

Before you begin

Role required: admin

Procedure:

1. Navigate to All > System Definition > Tables.
2. Open the table to delete.
3. Select Delete All Records.
Deleting all records before deleting the table ensures that the business logic is properly executed (for example, reference cascade rules or other delete business rules). If you do not delete all records from the table first, then you must manually fix any other records or tables that the table deletion impacts. Cascade deletions are also taken care of in this step.

4. Select Delete.
The Delete button is only available for custom tables beginning with u_ (global) or x_ (scoped).
5. In the confirmation dialog box, enter delete and click OK.


The table and all items that reference the table are deleted, including:

Choice list items
Forms, form sections, lists, and related lists
Reports and Performance Analytics widgets
Reference fields that reference the table
Access controls

 

Link to the product documentation -

https://www.servicenow.com/docs/bundle/xanadu-platform-administration/page/administer/table-administ...


**Please mark this as helpful, if it helps you.**

 

Regards,

Shubha S

Hitoshi Ozawa
Giga Sage
Giga Sage

@Anshika Singh There isn't an OOTB feature to automatically delete inactive tables after a certain days.

However, it's possible to create a scheduled job to check sys_created_on and sys_updated_on fields in each record to find newly created or updated records and also to query sys_audit table_delete table to find all tables that have delete operation done.

The operation is resource intensive so it should be executed with caution.

 

Following is a sample Script Include to find all inactive tables and then delete them.

var TableActivityChecker = Class.create();
TableActivityChecker.prototype = {
    initialize: function() {
    },

    findAndDeleteInactiveTables: function(daysAgo) {
        var thirtyDaysAgo = gs.daysAgoStart(daysAgo);
        var inactiveTables = [];
        
        var tableGr = new GlideRecord('sys_db_object');
        tableGr.addEncodedQuery('nameSTARTSWITHu_');  // Limit to custom tables
        tableGr.query();
        
        while (tableGr.next()) {
            var tableName = tableGr.name.toString();
            
            var gr = new GlideAggregate(tableName);
            gr.addAggregate('COUNT');
            gr.addEncodedQuery('sys_created_on>=' + thirtyDaysAgo + '^ORsys_updated_on>=' + thirtyDaysAgo);
            gr.query();
            gr.next();
            var activityCount = gr.getAggregate('COUNT');
    
            // Check deleted records (requires auditing to be enabled)
            var deleteCount = 0;
            var auditGr = new GlideRecord('sys_audit_delete');
            auditGr.addEncodedQuery('tablename=' + tableName + '^sys_created_on>=' + thirtyDaysAgo);
            auditGr.query();
            if (auditGr.hasNext()) {
                deleteCount = auditGr.getRowCount();
            }
    
            if (activityCount == 0 && deleteCount == 0) {
                inactiveTables.push(tableName);
                this.deleteRecordsAndTable(tableName);
            }
        }
        
        return inactiveTables;
    },

    // Method to delete all records from a table and then delete the table
    deleteRecordsAndTable: function(tableName) {
        try {
            // Delete all records from the table
            var gr = new GlideRecord(tableName);
            gr.query();
            while (gr.next()) {
                gr.deleteRecord();
            }

            // Delete the table itself
            var table = new GlideRecord('sys_db_object');
            if (table.get('name', tableName)) {
                table.deleteRecord();
                gs.log('Deleted table: ' + tableName);
            }
        } catch (e) {
            gs.error('Error while deleting records or table ' + tableName + ': ' + e.message);
        }
    },
    
    type: 'TableActivityChecker'
};

Can call it from following code to find all tables that has not been updated in past 30 days.

var tableActivityChecker = new TableActivityChecker();
var inactiveTables = tableActivityChecker.findAndDeleteInactiveTables(30);