- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-10-2023 02:00 AM
Hello,
I haven't used scheduled jobs till now and the requirement is to clean up the records which are from 6years ago.
How do I achieve this? we have a table that extends task table and there is a custom date field on this table (not the OOB sys_created_on) and all we have to do is to check this custom date field daily, if this field exceeds 6 years, the record has to be deleted/cleaned from the table. Can we use scheduled jobs for this? if yes how can this be done? can anyone help me with the scripting? are there any other methods this can be done?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-10-2023 02:43 AM
You can use archive rules that will move records from the custom table to an archive table. Furthermore, you can use destroy rules to permanently delete archived data you no longer need. Start here.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-10-2023 02:51 AM
There are multiple ways how you want to do it.
Either through archival rules or scheduled jobs.
This doc may guide you with archival rules : https://docs.servicenow.com/bundle/tokyo-platform-administration/page/administer/database-rotation/c...
Let me know if you need any help in scripting part of scheduled job apart from below
var today = new GlideDateTime();
today.addYearsUTC(-6);
var date = today.getDate();
var gr = new GlideRecord("tableName");
gr.addQuery("customer created fieldName","<",date);
gr.query();
while(gr.next()){
gr.deleteRecord();
}
Please mark correct if my response has solved your query.
Cheers,
Mohammed Basheer Ahmed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-10-2023 03:01 AM - edited ā01-10-2023 03:02 AM
Hi @Radhan Vasist
You can create a schedule job that runs daily.
var currentDate = new GlideDateTime();
currentDate.addYearsUTC(-6);
var previousDate = currentDate.getDate();
var gr = new GlideRecord("tableName");
gr.addQuery("Custom fieldName", "<" , previousDate);
gr.query();
gr.setWorkflow(false); // Bypass business rules and workflows
gr.deleteMultiple();
Please mark the suggestion as helpful, if you find it useful to you or others who wants to refer similar content.
Please mark the solution as correct, if the answer provided by me has resolved your query.
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-10-2023 02:43 AM
You can use archive rules that will move records from the custom table to an archive table. Furthermore, you can use destroy rules to permanently delete archived data you no longer need. Start here.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-10-2023 02:51 AM
There are multiple ways how you want to do it.
Either through archival rules or scheduled jobs.
This doc may guide you with archival rules : https://docs.servicenow.com/bundle/tokyo-platform-administration/page/administer/database-rotation/c...
Let me know if you need any help in scripting part of scheduled job apart from below
var today = new GlideDateTime();
today.addYearsUTC(-6);
var date = today.getDate();
var gr = new GlideRecord("tableName");
gr.addQuery("customer created fieldName","<",date);
gr.query();
while(gr.next()){
gr.deleteRecord();
}
Please mark correct if my response has solved your query.
Cheers,
Mohammed Basheer Ahmed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-10-2023 03:01 AM - edited ā01-10-2023 03:02 AM
Hi @Radhan Vasist
You can create a schedule job that runs daily.
var currentDate = new GlideDateTime();
currentDate.addYearsUTC(-6);
var previousDate = currentDate.getDate();
var gr = new GlideRecord("tableName");
gr.addQuery("Custom fieldName", "<" , previousDate);
gr.query();
gr.setWorkflow(false); // Bypass business rules and workflows
gr.deleteMultiple();
Please mark the suggestion as helpful, if you find it useful to you or others who wants to refer similar content.
Please mark the solution as correct, if the answer provided by me has resolved your query.
Thank you!