- Reasonably flexible
- Reasonably powerful
- Reasonably robust
This is a script I use currently to remove old jobs (mostly copied RupaliJ's post here).
List<CronTrigger> cron = [SELECT id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger where nextfiretime=null];
for(CronTrigger CT: cron)
} catch(Exception e1)
System.debug('--- Unable to delete scheduled job with Scheduled_Geocoder_Job_Id = ' + CT.id + ' because ' + e1.getMessage());
Judging by the criteria, this falls short for the following reasons:
- It cannot select job based on class used, job name, or type of job. With those attributes not being supported by the CronTrigger object, the power of the script to meet different needs is next to none.
- Since abortJob is considered a DML statement, this script will end after it has terminated 150 jobs. Usually you bulkify your processing for most Apex DML operations, but since abortJob () doesn't support collections as argument, there's no way to actually do bulk operations.
The need for an efficient way has been made more urgent by this mysterious issue (also here), as you may have to delete/recreate some/all your scheduled jobs just to deploy a new class (itself not even a Schedulable).
If you have a better approach, I'm definitely all ears.