Deleting ServiceNow Records via List Filter

When doing testing and development I frequently want to clean up all of the records between releases to give myself a fresh start. Using the list view is helpful, but can be slow if you have more than 100 or so records to delete.

The below script is run via the Scripts - Background view to take the results of your list view and delete the records, logging if they are successfully deleted or not with each one. It also supports a simulated delete if you want to see what records would be impacted.

Script Parameters

tableName The system name of the table your list view is using
[cmdb_ci_server] in my example
queryFilter The filter from the list
simulationMode true/false, should this simulate the deletion only

Grab the query filter with a quick right-click on the query

// ---------- Begin configuration section ---------- //var tableName = 'cmdb_ci_server';var queryFilter = 'nameLIKEcar';var simulationMode = true;// ---------- End configuration section ---------- //var gr = new GlideRecord(tableName);gr.addEncodedQuery(queryFilter);gr.query();while (gr.next()) {var recordDisplayName = gr.getDisplayValue();if (simulationMode) {gs.debug('Potentially deleted record: ' + recordDisplayName);} else {if (gr.deleteRecord()) {gs.debug('SUCCESS deleting record: ' + recordDisplayName);} else {gs.debug('FAILURE deleting record: ' + recordDisplayName);}}}

Previous
Previous

Prevent assembly locking in PowerShell

Next
Next

Step-By-Step Upgrade SCOM 1807 database to SQL Server 2017