Need to delete all records in sys_audit_delete table older 1 year

pavankumar970
Tera Contributor

Hi All,

 

 I need to delete all records in the table 'sys_audit_delete' older 1 year. Please help me..

2 REPLIES 2

Aniket Bhanse
Tera Guru

Here is the script for the same

 

You can enter the date as you want, and all the records which are created before that date will be deleted: 

var gr = new GlideRecord('sys_audit_delete');
gr.addEncodedQuery("sys_created_on<javascript&colon;gs.dateGenerate('2023-02-28','00:00:00')); //Enter the date here
gr.query();

gs.info(gr.getRowCount());

while(gr.next()){

gr.deleteRecord();

}

 

If my response helps you in any way, please mark my response as helpful or "Accept as Solution" so that your question will help others as well

Thank you

Adrian Ubeda
Mega Sage
Mega Sage

Hello @pavankumar970 , 
Adapt the query as your needs with filter builder, and then copy the query in this script (be sure that the query retrieves the data that you need before removing):

var grAudit = new GlideRecord('sys_audit_delete');
grAudit.addEncodedQuery('sys_updated_on<=javascript&colon;gs.beginningOfLast12Months()');
grAudit.query();
grAudit.deleteMultiple();

I would use deleteMultiple() function for this purpose, I let you and explanation over this: https://www.servicenow.com/community/developer-articles/servicenow-glide-record-deleterecord-vs-dele...

If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆