7

i have some post refresh steps after my full copy sandbox refresh where there is a task for deleting all the scheduled jobs so that the data is not changed. So i queried the crontrigger objects to see how many records and i have 40K jobs i need to delete which is not possible manually. So i wrote code as below

List<CronTrigger> listCronTrigger = [select Id from CronTrigger LIMIT 150];
  If (listCronTrigger.size() > 0)
  {
   For (Integer i = 0; i < listCronTrigger.size(); i++)
   { System.abortJob(listCronTrigger[i].Id); }
  }

Since the DML limit is 150 i am not able to delete more than 150 at a time. Can i know is there any easy way to achieve this?

2
  • 1
    Did you filter on the State to exclude those that were completed or error?
    – Eric
    Oct 18, 2015 at 0:23
  • 1
    a side note for future readers - System.abortJob(jobId) does DML - hence limits error
    – cropredy
    Dec 17, 2016 at 20:46

1 Answer 1

6

found the answer i wrote a batch class and executed it from developer console and it worked like a charm

Global class massDeleteScheduledJobs implements Database.Batchable<sObject> {
    // query all the scheduled jobs from the system
    String query = 'select Id from CronTrigger';
    global Database.QueryLocator start(Database.BatchableContext BC){
        /** Return all the scheduled jobs for processing  **/
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<CronTrigger> scope){
        If (scope.size() > 0){
        For (Integer i = 0; i < scope.size(); i++){ 
                System.abortJob(scope[i].Id); 
                }
            }
    }
    global void finish(Database.BatchableContext BC){

    }   
}
1
  • 2
    Pedantic note: size of scope will never be zero. Also, you should filter out jobs that will never be executed again so your job runs faster.
    – sfdcfox
    Oct 19, 2015 at 7:19

You must log in to answer this question.

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