Relations in the ServiceNow CMDB can easily get out of control, leading to performance problems and reduced data quality. To help address this, the script below provides a safe way to clean up the cmdb_rel_ci
table—for example, after mass-updating the Operational State of CIs or after mass-deleting CIs.
Because a ServiceNow CMDB can contain millions of relations—and because these relations drive impact analysis—it is critical to keep the table contents clean. When a CI is removed, its relations should also be removed. While I generally recommend retaining CIs that are no longer in use or discovered, I also advise removing relations that involve retired CIs. This prevents outdated dependencies from influencing impact calculations e.g. done by AI or OOTB flows, and user being able to select retired related items in operational processes.
With that in mind, I apply a simple rule:
- Every relation must have both a parent and a child.
- Both the parent and child must exist.
- Neither parent nor child may be Retired.
- All other relations are invalid and should be removed.
Note: ServiceNow does not support retiring relations, which actually makes sense.
That is precisely what the fix script below does—and more:
- Preview or Delete: Control behavior by setting
Preview Only
to true (preview) or false (delete). - Targeted Query: Identifies obsolete relations where the parent or child does not exist, or the operational status is 7 – Retired.
- Preview Mode: Generates a CSV report of all invalid relations (including Parent, Child, and Type).
- Delete Mode: Removes invalid relations in controlled “chunks” of 500 to avoid timeouts or performance issues.
- Summary: Displays the total number of deleted relations once completed.
- Rollback: Deletions can be reverted if required.
Disclaimer:
This script is provided ‘as is’ and without any warranty or guarantee of any kind, express or implied. Use this script at your own risk. The author assumes no responsibility or liability for any errors, issues, damages, or losses resulting from the use or misuse of this script.
TIP: Prevention is better than Fixing
If you have a CMDB with a lot of CIs, you may want to create a scheduled job that automatically removes obsolete/invalid relations daily or weekly. For CMDBs with a relatively low number of CIs and little turnaround, you could create a business rule that removes all relations where the current CI is either parent or child, when a CI is deleted or updated (i.e., on a real-time basis). Those using a Vancouver+ Release of ServiceNow should use the OOTB “CMDB (Relationship) Health” capability.
Fix Script to Clean Up Invalid/Obsolete CMDB Relationships:
📄 Sample Output
🔎 Preview Mode (previewOnly = true
)
Key points:
- Shows first N (configurable) invalid rows in CSV-style.
- Progress every 1000 processed.
- Final summary includes totals and runtime.
Delete Mode (previewOnly = false
)
Key points:
- No CSV rows are printed (only counts).
- Relationships are deleted in chunks.
- Each chunk deletion is logged.
- Final summary shows deleted count, total processed, and runtime.
Runbook
Purpose
This script identifies and removes invalid CMDB relationships in cmdb_rel_ci
.
Invalid relationships are those where:
- The parent CI is missing, or
- The child CI is missing, or
- The parent CI is Retired (
operational_status = 6
), or - The child CI is Retired (
operational_status = 6
).
Configuration Parameters
Located at the top of the script:
-
previewOnly
-
true
= Preview mode (no deletions, logs invalid relations) -
false
= Delete mode (removes invalid relations)
-
-
chunkSize
-
Number of records deleted per batch (default: 500).
-
Used only in Delete mode for safety.
-
-
retiredValue
-
Integer value for “Retired” (
6
by default).
-
-
previewLogLimit
-
Maximum invalid relations logged in Preview mode (default: 500).
-
Output is truncated if more are found.
-
-
progressStep
-
Log progress every X records processed (default: 1000).
-
Set to
0
to disable progress logging.
-
▶️ How to Run
Step 1 – Preview Mode (safe check)
-
Open Fix Scripts (or run in Background Scripts).
-
Paste the script and set:
-
Execute the script.
-
Check the System Log for output:
-
First 500 invalid relationships (configurable).
-
Total invalid relationships found.
-
Progress updates every 1000 processed.
-
Runtime in seconds.
-
-
If
No invalid relationships found
→ stop here, nothing to clean up.
Step 2 – Delete Mode (actual cleanup)
-
Confirm the Preview Mode results with your CMDB/data owner.
-
Change the config:
-
Execute the script.
-
Monitor the System Log:
-
Deletion occurs in safe chunks (default: 500).
-
Each chunk deletion is logged.
-
Summary shows total deleted + duration.
-
🔒 Safety Notes
-
Always run Preview Mode first.
-
Review output with CMDB/data owners before deletion.
-
Adjust
chunkSize
for very large CMDBs to avoid timeouts. -
Script logs a start timestamp, progress, and total runtime for traceability.
Leave A Comment