Best Practice : Record Deletion

Abc 26
Tera Expert

Hello Experts,

 

I want delete records from a table which are older than 30 days . I want to have records of only last 30 days and not older than that.

 

I want to the perform this action every week on Sunday. what could be the best practice to implement the above scenario.

 

Thanks

3 REPLIES 3

Anurag Tripathi
Mega Patron
Mega Patron

You should look at Table Rotation -> Check this link Demystifying table rotation, extension, and table ... - ServiceNow Community

-Anurag

OlaN
Giga Sage
Giga Sage

Hi,

You could set up a simple scheduled Flow to regularly delete old records.

Simple example below.

flow-delete-old-records.png

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Abc 26 ,
I trust you are doing fine.
To delete records from a table that are older than 30 days, you can create a scheduled job that runs a script to delete the records. Here's an example script that you can modify to fit your specific table name and field for date tracking:

var daysAgo = new GlideDateTime();
daysAgo.addDays(-30);
var query = 'sys_created_on<'+daysAgo.getValue();
var gr = new GlideRecord('your_table_name_here');
gr.addQuery(query);
gr.deleteMultiple();

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi