How to delete the records using scheduled job
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2023 03:08 AM
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2023 03:42 AM
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();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2023 03:45 AM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2023 03:49 AM
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.