Aircraft by Modelmodel-aircrafts.com

Glide Record Delete ServiceNow: How to Remove Records

Using GlideRecord delete in ServiceNow removes records directly from the database without triggering standard workflow automation. This method bypasses business rules and scripts, making it fast but risky for data integrity. You must use it only when you are certain that no downstream processes need to run.

How GlideRecord Delete Works

The GlideRecord object allows you to interact with database tables programmatically. When you call the delete method on a specific record instance, the system issues a direct SQL delete statement. It does not fire before or after insert, update, or delete scripts. It also skips any approval workflows or notification rules tied to the table. This behavior is why many developers avoid it for routine operations.

You typically load a record using its sys_id, then call delete on that object. The system removes the row immediately. If you try to delete a record that does not exist, the method returns false. It does not throw an error. This silent failure can make debugging difficult if you expect the deletion to happen every time.

Common Misconception

A common belief is that GlideRecord delete is always safe because it works like clicking the trash icon in the user interface. This is incorrect. The UI triggers full application logic, including security checks and audit trails. The script method does not always apply the same level of validation. You must ensure your script handles all necessary checks manually.

When to Use Direct Deletion

You should use this approach primarily for bulk cleanup tasks or system maintenance. Examples include removing old log entries, clearing temporary session data, or archiving completed transactions. In these cases, speed is more important than triggering notifications or approvals. You want the records gone without waiting for background jobs to process them.

Do not use it for user-facing data changes. If a customer cancels an order, you need the cancellation workflow to run. This ensures inventory updates, refunds, and emails are sent correctly. Using GlideRecord delete here would leave the system in an inconsistent state. The order might show as cancelled to the user, but the warehouse would still think it needs to ship the items.

Worked Example

Imagine you have a custom table called “Temp Data” used for batch processing. After a job finishes, you need to clear all rows older than one hour. A script can find these records and remove them efficiently.

First, you create a GlideRecord for the temp_data table. You add a query filter to find records where the creation date is less than the current time minus one hour. You execute the query. Then, you loop through each result. Inside the loop, you call delete on the current record. This removes each row one by one. The loop continues until all matching records are gone. This method is much faster than deleting records one at a time through the UI.

Security and Access Control

Even though GlideRecord delete bypasses business rules, it still respects access control lists. If the running user does not have delete permissions on the table, the operation fails silently. The record remains in the database. The script does not crash, but the deletion does not happen. This can lead to confusion where you think the code worked, but the data persists.

You must ensure the user context has the correct roles. Typically, this means the user needs the delete role for the specific table. For system-level tasks, you might use the admin user or a dedicated service account. Always test your script with a limited user first to verify permissions work as expected.

Handling Dependencies

Some tables have foreign key constraints or referential integrity rules. Deleting a parent record might require deleting child records first. GlideRecord delete does not automatically handle cascading deletes unless configured in the database schema. You may need to write additional logic to remove related rows. Failing to do so can cause orphaned records or database errors.

Check the table structure in the Application Registry. Look for relationships defined between tables. If a relationship is set to cascade delete, the database handles it. If not, your script must manage the order of deletion. Start with the most dependent tables and work your way up to the main table.

Alternatives for Safer Deletion

If you need to preserve audit trails or trigger workflows, consider using the standard API methods instead. The `update()` method followed by setting a status field to “Deleted” is a common pattern. This keeps the record in the database but marks it as inactive. Business rules can still run based on this status change.

Another option is to use the `deleteMultiple()` method on a GlideRecord collection. This removes all records that match a query in one go. It is slightly faster than looping and calling delete individually. However, it still bypasses individual record scripts. Use it only when you are sure no per-record logic is needed.

Best Practices for Maintenance

Always wrap deletion scripts in try-catch blocks. This helps you handle unexpected errors gracefully. Log the number of records deleted for auditing purposes. This makes it easier to track how much data was removed during a maintenance window. It also helps identify if a script failed partway through.

Test your deletion logic in a development instance first. Verify that the correct records are removed and that no unintended side effects occur. Check related tables to ensure no orphaned data remains. Once verified, you can safely deploy the script to production.

GlideRecord Delete for Bulk Cleanup Tasks

When dealing with large volumes of data, performance becomes a critical factor. Loading all records into memory can cause timeout errors. To avoid this, process records in smaller batches. Limit your query to a manageable number of rows at a time. Loop through the batches and delete them sequentially.

This approach reduces memory usage and prevents the system from becoming unresponsive. It also allows other users to continue working while the cleanup runs. Schedule these tasks during off-peak hours to minimize impact on overall system performance. Monitor the job queue to ensure the task completes successfully.

Related

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