Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Core ServiceNow APIs/GlideRecord/Safe Bulk Delete/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# GlideRecord Bulk Delete with Safety Checks

## Description
This snippet allows you to safely delete multiple records from a ServiceNow table based on an encoded query.
It logs all records that match the query so you can review them before actually deleting anything.
Helps prevent accidental mass deletion of important data.

## Note
- Works in Global Scope by default
- Can be executed in Background Scripts or Script Includes
- **ALWAYS REVIEW LOGS BEFORE ENABLING DELETION**
## Prerequisites
- Server-side context (Background Script, Business Rule, Script Include)
- Access to the target table
- Basic understanding of GlideRecord and Encoded Queries

## Usage
```javascript
// Logs all active low-priority incidents that would be deleted
safeDelete('incident', 'active=true^priority=5');

// To perform actual deletion, uncomment gr.deleteRecord() inside the function
```

## Output
```
Records matching query: 3
Record sys_id: 12345abcdef would be deleted.
Record sys_id: 23456bcdef would be deleted.
Record sys_id: 34567cdefg would be deleted.
Bulk delete preview complete. Verify logs before enabling deletion.
```
33 changes: 33 additions & 0 deletions Core ServiceNow APIs/GlideRecord/Safe Bulk Delete/safeDelete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Safely delete multiple records from a ServiceNow table.
* Logs all affected records before deletion to prevent accidental data loss.
* Uncomment gr.deleteRecord() to perform the actual deletion.
*
* @param {string} table - The table name
* @param {string} encodedQuery - GlideRecord encoded query for filtering records
*/
function safeDelete(table, encodedQuery) {
if (!table || !encodedQuery) {
gs.error('Both table name and encoded query are required.');
return;
}

var gr = new GlideRecord(table);
gr.addEncodedQuery(encodedQuery);
gr.query();

var count = gr.getRowCount();
gs.info('Records matching query: ' + count);

if (count === 0) {
gs.info('No records found. Nothing to delete.');
return;
}

while (gr.next()) {
gs.info('Record sys_id: ' + gr.getValue('sys_id') + ' would be deleted.');
// gr.deleteRecord(); // Uncomment this line to actually delete
}

gs.info('Bulk delete preview complete. Verify logs before enabling deletion.');
}
Loading