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

Community Alums
Not applicable

You can do this with a scheduled job. Run a query to get the list of records that match your criteria (closed_at >12 months), then iterate through the list and delete each record. Start here to get an idea of how to query and delete using GlideRecord:



GlideRecord Query Cheat Sheet - ServiceNow Guru



Cheers,



Tim


Lucas Vieites
Tera Guru

Hi Leah,


instead of deleting the mentioned data, have you thought of Archiving (see: Archiving Data - ServiceNow Wiki   ). If the deletion is really the only way, you could use the provided Table Cleaner functionality as described in Introduction to Managing Data - ServiceNow Wiki



I hope this answers your question.



Regards,


Lucas Vieites


LMHoffman
Kilo Expert

Our company wants to purge the data for anything >12 months, and they want it on a monthly rolling schedule. Is there some way this can be scripted on the incident, problem and change tables to delete anything >12 months old?


Community Alums
Not applicable

You can use something like this (scripts-background to test result set):



//Find all inactive incident records and print them one-by-one


var gr = new GlideRecord('incident');


gr.addQuery('active',false);


gr.addQuery('closed_at','<=','javascript:gs.monthsAgo(12)');


gr.query();


while (gr.next()) {


    //Print each record in the query result set


    gs.print(gr.number);


}




The actual command to delete the record would be:



//Find all inactive incident records and delete them one-by-one


var gr = new GlideRecord('incident');


gr.addQuery('active',false);


gr.addQuery('closed_at','<=','javascript:gs.monthsAgo(12)');


gr.query();


while (gr.next()) {


    //Delete each record in the query result set


    gr.deleteRecord();


}



Keep in mind, your criteria might be different. As an example, are you only wanting to delete Incidents/Problems/Changes which are actually closed, or is it inclusive of all records, regardless of state?



Cheers,



Tim