Cleaning table records after some years

Radhan Vasist
Tera Contributor

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?

3 ACCEPTED SOLUTIONS

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

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.

View solution in original post

Basheer
Mega Sage

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 hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

View solution in original post

Neeraj Modi
Tera Guru

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!

View solution in original post

3 REPLIES 3

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

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.

Basheer
Mega Sage

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 hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

Neeraj Modi
Tera Guru

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!