From 043ec22c1bc0e181dccf3422580ae9bf61a5836f Mon Sep 17 00:00:00 2001 From: Thrizvi <145013431+Thrizvi@users.noreply.github.com> Date: Mon, 13 Oct 2025 12:20:46 +0530 Subject: [PATCH 1/2] Create Delete Incident which are old This script is designed to run in the **Scripts - Background** module of ServiceNow. It identifies and deletes Incident records that were opened more than 90 days ago from the current date. --- .../Old Incident/Delete Incident which are old | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Server-Side Components/Background Scripts/Old Incident/Delete Incident which are old diff --git a/Server-Side Components/Background Scripts/Old Incident/Delete Incident which are old b/Server-Side Components/Background Scripts/Old Incident/Delete Incident which are old new file mode 100644 index 0000000000..aae1a3c8e3 --- /dev/null +++ b/Server-Side Components/Background Scripts/Old Incident/Delete Incident which are old @@ -0,0 +1,17 @@ +(function() { + var ninetyDaysAgo = new GlideDateTime(); + ninetyDaysAgo.subtract(1000 * 60 * 60 * 24 * 90); // 90 days in milliseconds + + var gr = new GlideRecord('incident'); + gr.addQuery('opened_at', '<', ninetyDaysAgo); + gr.query(); + + var count = 0; + while (gr.next()) { + gs.info('Deleting Incident: ' + gr.number); + gr.deleteRecord(); + count++; + } + + gs.info('Total Incidents Deleted: ' + count); +})(); From 63a6c93bd7f78f2a6482f76335d80a2b53e419bb Mon Sep 17 00:00:00 2001 From: Thrizvi <145013431+Thrizvi@users.noreply.github.com> Date: Mon, 13 Oct 2025 12:21:28 +0530 Subject: [PATCH 2/2] Create Readme.md This script is designed to run in the **Scripts - Background** module of ServiceNow. It identifies and deletes Incident records that were opened more than 90 days ago from the current date. --- .../Background Scripts/Old Incident/Readme.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Server-Side Components/Background Scripts/Old Incident/Readme.md diff --git a/Server-Side Components/Background Scripts/Old Incident/Readme.md b/Server-Side Components/Background Scripts/Old Incident/Readme.md new file mode 100644 index 0000000000..161efc1985 --- /dev/null +++ b/Server-Side Components/Background Scripts/Old Incident/Readme.md @@ -0,0 +1,28 @@ +# Delete Incidents Older Than 90 Days – ServiceNow Script + +This script is designed to run in the **Scripts - Background** module of ServiceNow. It identifies and deletes Incident records that were opened more than 90 days ago from the current date. + +## 🧩 Purpose + +To clean up old Incident records that are no longer relevant or needed, helping maintain a lean and efficient ServiceNow instance. + +## 🛠️ Script Overview + + +(function() { + var ninetyDaysAgo = new GlideDateTime(); + ninetyDaysAgo.subtract(1000 * 60 * 60 * 24 * 90); // 90 days in milliseconds + + var gr = new GlideRecord('incident'); + gr.addQuery('opened_at', '<', ninetyDaysAgo); + gr.query(); + + var count = 0; + while (gr.next()) { + gs.info('Deleting Incident: ' + gr.number); + gr.deleteRecord(); + count++; + } + + gs.info('Total Incidents Deleted: ' + count); +})();