Aircraft by Modelmodel-aircrafts.com

GlideRecord Delete: How to Remove Records in ServiceNow

To delete a record with GlideRecord, query for the record, call next() to load it, then call deleteRecord() on the GlideRecord object. The main condition is that the user or role running the script must have delete rights on that table, or the call fails quietly and the record stays put. If you want to wipe every record matching a query without loading them, use deleteMultiple() instead.

The basic delete call

Build the GlideRecord against the table you care about, add a query that pins down the record, and step through the result. The pattern looks like this in a server script:

var gr = new GlideRecord(‘incident’);
gr.addQuery(‘number’, ‘INC0010001’);
gr.query();
if (gr.next()) {
gr.deleteRecord();
}

The if check matters. If the query returns nothing, next() returns false, and calling deleteRecord() on an empty GlideRecord does nothing useful. Checking first also lets you log or handle the not-found case instead of assuming the delete worked.

Deleting many records

When you need to remove a set of records, you have a choice between a loop and a single call. The loop version queries, then calls deleteRecord() inside a while loop:

var gr = new GlideRecord(‘incident’);
gr.addQuery(‘state’, ‘8’);
gr.query();
while (gr.next()) {
gr.deleteRecord();
}

The single-call version replaces the loop with deleteMultiple():

var gr = new GlideRecord(‘incident’);
gr.addQuery(‘state’, ‘8’);
gr.deleteMultiple();

deleteMultiple() runs the query and removes every matching row without stepping through them in script. That makes it faster on large sets, but it also means you cannot inspect each record before it goes. Business rules still fire for each deleted record with either approach, which is the next thing to understand.

What runs during a delete

Deleting a record is not just a row disappearing from the database. Each delete triggers the table’s business rules, and a before business rule can cancel the delete by setting abortAction. So a delete that returns without error may still have been blocked by a rule. If records refuse to disappear, check the business rules on the table before blaming your script.

Related records can go too. If the table has cascade rules configured, deleting a parent removes its child records as well. The classic example is deleting an incident and finding its incident task records gone with it. Whether a relationship cascades is set on the dictionary entry for the reference field, so check there if you are unsure what a delete will drag along.

Also know that deleteRecord() and deleteMultiple() are server-side methods. Calling them in a client script does nothing, because GlideRecord in the browser is read-only. Client scripts that need to delete must ask the server through a script include, a REST call, or GlideAjax.

Permissions and safer options

The delete honors access controls. If the script runs as a user without the delete operation on that table, the record survives and no error is thrown at you. Scripts running with elevated privileges can bypass that check, which is convenient and dangerous in equal measure. Before running a broad deleteMultiple(), test the query with a count or a read-only loop and look at what it matches.

For anything you might want back, consider a softer approach first. Many tables support a flow that deactivates or closes records instead of deleting them, which keeps history intact for reporting and auditing. If your instance has some form of record restoration available, deletions may be recoverable, but treat that as a safety net rather than a plan. The dependable safety net is a tested query and a backup of the data you are about to remove.

A worked walk-through

Suppose you have a custom table full of temporary import staging rows, and you want to clear the ones flagged as finished. The safe sequence is: first run the same query with a loop that only logs the record numbers, so you can eyeball the match set. Then swap the log line for deleteRecord() inside the loop, or replace the loop with deleteMultiple() once you trust the query. If the staging table has a reference from another table pointing into it, check whether that reference cascades before running the bulk delete, or you may take out rows you meant to keep. After the run, spot-check the table and the related tables rather than trusting the script’s silence.

Deleting from related lists and UI actions

A common follow-up is deleting a record from a form button rather than a background script. The usual route is a UI action on the table: it can run a server-side script that builds the GlideRecord, queries by the current record’s sys_id, and calls deleteRecord(). The same permission rules apply, so a user without delete rights will see the button do nothing unless the action is set to run with elevated rights. For deleting a related-list row from the parent form, query the child table by the parent reference field, then delete the matching row. The misconception to avoid here is thinking deleteRecord() on the parent GlideRecord removes a child you have loaded. It does not. Each GlideRecord is bound to one table, and the delete always targets that table.

Related

Published 1 December 2026. Found an error? Tell us.