Blog

Deletion Methods

Creating data is easy, sometimes deleting it is difficult.  Questions that might occur with deletion:

  • How do I delete a large number of rows?
  • When do deletions cascade?
  • How do I get access to delete?
  • What happens when the browser times out?
  • How do I delete an entire table?
  • How do I delete via a script?

This article answers those questions and also maybe shows a few deletion secrets you didn't know!


1.  Delete Button

Most ServiceNow forms contain the Delete Button.  You can use this to delete a single record.  

This button is created by the global UI Action, Delete.

Visibility of this button is controlled by access controls.  Often the delete button is restricted to an administrative role like admin or itil_admin

Also note that when you delete something, delete cascade rules might apply.

Incident Delete Button (Click to expand)

Incident Delete Access Control (Click to expand)


2. Delete from List

Most ServiceNow lists contain the Delete "List Context Menu".  You can use this to delete multiple records at a time.

This "list context menu" button is created by the global UI Action, Delete.

Visibility of the list context option, Delete, is controlled by access controls.  Often the delete button is restricted to an administrative role like admin or itil_admin.

Notes on: Rows Per Page

To add more rows visible on a list, you can adjust the title bar.

To change the available rows per page options, navigate to System Properties > System and locate the property 'Items per page' drop-down options (comma separated, no spaces). Enter the values you want to make available to users.

Embedded lists are a little different, you click the red x next to the row to delete.

Incident Delete by List (Click to expand)

Incident Delete List Context Menu (Click to expand)


3. Delete from Tables and Columns

Tables & Columns Delete all Records (Click to expand)

To delete an entire table, you can do so from Tables & Columns.  To delete a table from Rows and Columns:

1. Navigate to System Definition > Tables & Columns.
2. In the Table Names pane, select a table. A list of the columns on the selected table appears in the Column Names pane.
3. Click Delete all Records button


4. Delete from Background Script

Deleting from scripts gives the advantage of querying and deleting only certain records. Also by using the setWorkflow statement, you can ignore business rules when deleting.

Of course you want to be careful using this.  Always test in a non-production instance first!

1. Login as an admin with the security_admin role
2. Elevate your privilege to security_admin
3. Go to System Definition > Scripts - Background
4. Run your statement.  Example statement below

removeComputers();
function removeComputers() {
 var gr = new GlideRecord("cmdb_ci_computer");
 gr.addEncodedQuery('sys_created_by=mikekaufman')
 gr.query();
 var deleteCount = 0;
 while(gr.next()){
gr.setWorkflow(false);
 //gr.deleteRecord();
 deleteCount++;
 }
 gs.print('Records Deleted: '+ deleteCount);
}

In the example above, it delete computers created by mikekaufman.  I always leave the deleteRecord line commented out until I am sure this statement will delete the appropriate amount of records.  Then I uncomment the deleteRecord line and run the script.


5. Delete from SQL

Don't do this, use the other methods instead.  However here is how to do this.

1. Login as an admin with the security_admin role
2. Elevate your privilege to security_admin
3. Go to System Definition > Scripts - Background
4. Run your statement.  Example statement below (replace <table> with the table you want to delete)

gs.sql('truncate <table>');

NOTE: This ignores all business rules and workflow.  It completely deletes all records in the table.  You can only restore records from backup if you do this.


6. Removing a Bad Comment or Work Note

Made an inappropriate comment?  You can still maybe save yourself by following these steps to remove the comment.

1. Right-click on the record and select Copy URL to Clipboard to obtain the unique sys_id of the record. For example: https://.service-now.com/nav_to.do?uri=incident.do?sys_id=85befb1c4a34bb12013b216a9fd5fee8
2. Copy the sys_id. In the URL example above, the sys_id is: 85befb1c4a34bb12013b216a9fd5fee8.
3. Update the Journal Entry.

  • Enter the journal entry URL + the sys_id. For example: https://.servicenow.com/sys_journal_field_list.do?sysparm_query=element_id=85befb1c4a34bb12013b216a9fd5fee8
  • Locate the journal entry.
  • Modify the record and update or delete the record.

4. Update the Audit Entry.

  • Enter the audit entry URL + the sys_id. For example: https://.servicenow.com/sys_audit_list.do?sysparm_query=documentkey=85befb1c4a34bb12013b216a9fd5fee8
  • Locate the journal entry.
  • Modify the record and update or delete the record.

5. Rebuild the History Set. This is only required when you do not use direct
auditing and the property glide.sys.activity_using_audit_direct is
set to false.

  • Locate all History Set records for the item whose history needs to be rebuilt by entering the history set URL + the sys_id. For example: https://.service-now.com/sys_history_set_list.do?sysparm_query=id=85befb1c4a34bb12013b216a9fd5fee8
  • Click the Delete button for each History Set. This will delete the History Set, not the audit data. The History Set will be rebuilt with the corrected audit and journal information as soon as a user views the item.

7. Delete Multiple

You can also use the deleteMultiple GlideRecord method now if you are deleting a lot of records.


Restore Records

You should take many precautions when deleting records, don't start a delete if you didn't mean it.  Unfortunately I know that in the real world we make mistakes.

If you did a gs.sql statement, you might need to start a restore from backup or from another instance.  However if you did one of the other types of deletes, you can restore the records using the Deleted Records module.

You can also stop an active script by killing an active transaction.


Session Timeouts

Sometimes when you run a large script, update, or deletion, ServiceNow will process and you are prevented from continuing that browser session until it is completed.  On very large transactions, the browser can timeout.

That is ok, when ServiceNow times out or processes, it will continue to run when your browser is closed.

If you are an impatient or busy person, you can always open a different browser to continue to work in ServiceNow as the transaction processes.  You can also use Google Chrome's incognito mode to open up another session of ServiceNow as well.