Record Cleanup
Learn how to automate ServiceNow record maintenance and cleanup tasks using Ansible to query and close records systematically.
Introduction
As part of good ITSM hygiene, it’s important to maintain clean and current records in ServiceNow. This module demonstrates how to automate the cleanup of records created during the workshop, showcasing techniques that can be applied to regular maintenance tasks in production environments.
Record Cleanup Scenarios
Automated record cleanup is useful for:
-
Workshop/Training Cleanup: Removing test data after learning exercises
-
Periodic Maintenance: Closing stale or abandoned records
-
Data Governance: Ensuring records follow proper lifecycle management
-
Compliance: Meeting retention and cleanup requirements
Cleanup Strategy
The cleanup process involves several steps:
-
Discovery: Query for records matching specific criteria
-
Validation: Ensure records are appropriate for cleanup
-
Processing: Update or close records systematically
-
Verification: Confirm cleanup operations completed successfully
Review the Cleanup Playbook
A specialized playbook called close-records-by-user.yml has been created for this cleanup operation.
-
Open the VS Code tab in your lab environment
-
Locate and open the
close-records-by-user.ymlfile -
Review the playbook structure and logic
-
Pay attention to the different module types used
Understanding Info Modules
The playbook uses several *_info modules that provide query capabilities:
-
servicenow.itsm.incident_info: Queries incident records -
servicenow.itsm.problem_info: Searches problem records -
servicenow.itsm.change_request_info: Finds change request records
These modules search for active records created by your username and return structured data for processing.
Data Transformation Process
The playbook demonstrates advanced Ansible techniques:
-
Query Execution: Info modules retrieve relevant records
-
Data Processing: Ansible transforms returned data into manageable lists
-
Record Updates: Lists are passed to update modules for bulk processing
-
Field Handling: Uses the
otherparameter for custom or non-standard fields
Query and Close Records
Execute the cleanup automation to systematically close all records created during the workshop.
Cleanup Execution
-
Navigate to the Automation Controller tab
-
Go to Automation Execution → Templates
-
Locate the
5 - Query and close records by user (close-records-by-user.yml)job template -
Click the rocket icon (🚀) to launch the cleanup job
-
Monitor the job output carefully during execution
Monitoring the Cleanup Process
As the job runs, observe the following phases:
-
Discovery Phase: Queries identify records for cleanup
-
Incident records matching your username
-
Problem records created by you
-
Change requests under your name
-
-
Validation Phase: Confirms records are appropriate for closure
-
Verifies record ownership
-
Checks current status eligibility
-
Validates business rules compliance
-
-
Processing Phase: Updates and closes identified records
-
Updates incident status to "Resolved" or "Closed"
-
Closes problem records with appropriate resolution
-
Completes change requests with implementation status
-
-
Reporting Phase: Provides summary of cleanup actions
-
Lists all affected record numbers
-
Reports success/failure status
-
Documents any errors or exceptions
-
Inspect Results
Verify that all workshop records have been successfully closed or removed from active status.
Verification Process
-
Wait for the cleanup job to complete successfully
-
Review the job output for the summary of actions taken
-
Note all record numbers that were processed
-
Access the ServiceNow tab to verify the changes
ServiceNow Verification
Check each record type to confirm cleanup:
-
Navigate to Self-service - Incidents in ServiceNow
-
Look for incidents created during the workshop
-
Verify they now show "Resolved" or "Closed" status
-
Check that closure notes indicate automated cleanup
-
Access the Problems view in ServiceNow
-
Search for problems created during Modules 2-3
-
Confirm they show appropriate closed status
-
Verify resolution details are populated
-
Go to Change - Open or Change - All views
-
Locate change requests from Module 3
-
Verify they show completed or closed status
-
Check implementation and closure details
Expected Cleanup Results
After successful cleanup, you should observe:
| Record Type | Expected State |
|---|---|
Incidents |
Status: Resolved/Closed, Resolution: Automated cleanup |
Problems |
State: Closed, Resolution: Workshop completion |
Change Requests |
State: Closed/Complete, Implementation: Automated |
Configuration Items |
Remain active (CIs typically aren’t "closed") |
Understanding Cleanup Automation
This module demonstrates several important concepts:
Bulk Operations
-
Efficient Processing: Handle multiple records in a single operation
-
Consistent Updates: Apply standardized closure procedures
-
Error Handling: Manage individual record failures gracefully
-
Audit Trail: Maintain complete records of automated actions
Production Applications
Similar techniques can be used for:
-
Scheduled Maintenance: Regular cleanup of old or stale records
-
Data Lifecycle Management: Implementing retention policies
-
Compliance Automation: Meeting regulatory cleanup requirements
-
Environment Management: Sanitizing test/development instances
Workshop Cleanup Complete
With all workshop records properly closed and cleaned up, your ServiceNow instance is ready for the final module. The cleanup process demonstrates how automation can maintain data hygiene and implement consistent record lifecycle management.
Next Steps
You’re now ready to proceed to Module 6, the final module of this workshop, where you’ll explore how to use ServiceNow as a dynamic inventory source for Ansible operations.
Troubleshooting
If you have encountered an issue or have noticed something not quite right, please open an issue on GitHub.