How can I delete tickets >12 months old?

LMHoffman
Kilo Expert

Have a requirement to delete incidents, problems, and changes >12 months old and I don't know how to do it.

13 REPLIES 13

ghsrikanth
Tera Guru

Please create a scheduled script execution, to get more info refer the below link -


Creating a Scheduled Job - ServiceNow Wiki



In the script section, please write the script as below - I would like Tim to review



var enq = "sys_updated_onRELATIVELT@month@ago@12";



var grInc = new GlideRecord('incident');


grInc.addEncodedQuery(enq);


grInc.query();


grInc.deleteMultiple();



var grChg = new GlideRecord('change_request');


grChg.addEncodedQuery(enq);


grChg.query();


grChg.deleteMultiple();



var grPrb = new GlideRecord('problem');


grPrb.addEncodedQuery(enq);


grPrb.query();


grPrb.deleteMultiple();



You can schedule this script to run every month regularly and this will automatically delete the 12 months old records.


Make sure that you are sure about deletion, because this wiil remove the records. Its preferred to make a "soft" delete, that is making Active field as False


and wait for one more month and then delete.


But its your decision, the above script will do the trick.



Mark if it is helpful or correct, feedback is appreciated


Community Alums
Not applicable

I agree, encoded query is the better route.



Cheers,



Tim


The query() call is not really needed when you want to delete data. You can just call directly deleteMultiple().



I also think customer refers to data created before last 12 months, not updated.



Regards,


Sergiu


peterdelf
Giga Expert

For a straight-up deletion, use the out-of-box Table Cleaner functionality and add a line to the Auto Flushes [sys_auto_flush] table.