Skip to content

Schedule job for cleanup data #1106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
28 changes: 28 additions & 0 deletions Scheduled Jobs/Schedule Job For Data Clean Up/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
**Overview :**
This script automates the cleanup of old closed incidents in the ServiceNow instance. It helps maintain a clean and efficient database by removing records that are no longer needed, thereby improving system performance and manageability.


**Features**

**Automatic Cleanup**: Deletes closed incidents that are older than 90 days.

**Customizable Timeframe**: Easily adjustable parameters to change the age of incidents for cleanup.

**Logging:** Outputs the number of incidents deleted to the system logs for tracking purposes.


**Prerequisites**
Access to a ServiceNow instance with appropriate permissions to run scripts.
Knowledge of how to create Scheduled Jobs in ServiceNow.


**Installation**
**Create a New Scheduled Job:**
Navigate to System Definition > Scheduled Jobs in your ServiceNow instance.
Click on New to create a new Scheduled Job.


**Configure the Job:**
**Name**: Enter a descriptive name for the job, e.g., "Cleanup Old Closed Incidents."
**Run**: Set the desired schedule for when you want the job to run (e.g., daily, weekly).
**Script**: Copy and paste the provided javascript into the script section of the Scheduled Job.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var gr = new GlideRecord('incident');
gr.addQuery('sys_created_on', '<=', gs.daysAgo(90));
gr.addQuery('state', 'Closed');
gr.deleteMultiple();
gs.info('Deleted old closed incidents older than 90 days.');
Loading