How to delete the records using scheduled job

Sanket Pawar
Tera Contributor

Hi All,

I have a requirement where, the schedule job should run weekly once and should find knowledge article usages table (kb_use) and delete records that are more than 2 years old (created more than 2 year ago).

 

3 REPLIES 3

Community Alums
Not applicable

Hi @Sanket Pawar ,

Refer to this Doc Schedule or execute a job to delete records 

For the script, you can try something like this example :

try 

var gr = new GlideRecord('incident')
gr.addEncodedQuery('end_dateONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()'); //to delete all inactive incidents
gr.deleteMultiple();

SatyakiBose
Mega Sage

Hello @Sanket Pawar 

You can run the below script by changing the table, and the time window as required:

var gr = new GlideRecord('change_request');
gr.addQuery('opened_atONLast year@javascript:gs.beginningOfLastYear()@javascript:gs.endOfLastYear()');
gr.query();
while (gr.next()) {
     //Delete each record in the query result set
     gr.deleteRecord();
}

 

Rajesh Chopade1
Mega Sage

hi @Sanket Pawar 

 

If you are deleting multiple records then the 'deleteMultiple' method can be used as a shortcut,

 

Here is a sample to delete all inactive incident :

 

/Find all inactive incidents and delete them all at once


var gr = new GlideRecord('incident'); // you can put here your table name 'kb_use'

gr.addQuery('active', false); // add your query here or use addEncodedQuery 

gr.deleteMultiple(); //Deletes all records in the record set

 

I hope this will helps you

 

Thank you

Rajesh.