Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Purpose
This script automates the cleanup of historical records (closed incidents older than 90 days) while preserving records linked to active change requests or tasks. This helps improve system performance and database maintenance.

## Use Case
- Remove outdated incidents that are no longer required.
- Prevent accidental deletion of records linked to other critical tables.
- Can be extended to other tables like `problem`, `change_request`, etc.

## Script Details
- **Table:** `incident`
- **Filter Criteria:**
- Closed incidents (`state = 7`)
- Older than 90 days (`closed_at <= gs.daysAgo(90)`)
- **Safety Checks:** Skips records linked to `change_request` or child `task` records.
- **Execution:** Can be run as Background Script or Scheduled Script Execution.
- **Logging:** Outputs skipped and deleted records count in system logs.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var gr = new GlideRecord('incident');
gr.addQuery('state', 7); // Closed incidents
gr.addQuery('closed_at', '<=', gs.daysAgo(90));
gr.query();

var deletedCount = 0;

while (gr.next()) {

// Check for any child tasks or linked changes
var related = new GlideRecord('task');
related.addQuery('parent', gr.sys_id);
related.query();

if (related.next()) {
gs.info('Skipping ' + gr.number + ' because it has linked tasks/changes.');
continue;
}

// Safe to delete
gr.deleteRecord();
deletedCount++;
}

gs.info('Cleanup completed. Total records deleted: ' + deletedCount);
Loading