1

I have a few apex jobs which become stale (i.e. they just lie around in scheduled jobs queue with no nextfiretime) if an exception is hit while running the schedulable class. I cannot abort these jobs using the System.abort(jobId) since the job is not running. What is the best way to delete such stuck/erroneous apex jobs with no next fire time? Should I just query the cronTrigger objects and delete them?

List<CronTrigger> ctriggers = [SELECT Id, NextFireTime FROM CronTrigger
                      WHERE CronJobDetail.Name like 'JobName%'
                      And NextFireTime = NULL];

delete ctriggers;
4
  • Have you tried it?
    – Adrian Larson
    Mar 7, 2017 at 18:22
  • I tried deleting the cronTrigger objects but hit the following DML exception -> DML operation DELETE not allowed on CronTrigger
    – gerad26
    Mar 7, 2017 at 18:33
  • And what happened when you called abortJob? This is information you can edit into your post to make it easier to answer.
    – Adrian Larson
    Mar 7, 2017 at 18:36
  • N.B. Per SFDC Object ref, CronTrigger or AsyncApexJob doesn't support delete call
    – cropredy
    Mar 7, 2017 at 19:37

1 Answer 1

3

I seem to think I've had this issue before and solved it using, e.g.:

System.purgeOldAsyncJobs(Date.today().addMonths(-12));

You might want to test it on a smaller date range though.

See docs for details.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .