+ Start a Discussion
HNT_NeoHNT_Neo 

Delete apex batch job

I’m trying to delete a scheduled apex batch job using the syntax below. However, the syntax will delete all scheduled apex jobs, is it possible to delete just 1 scheduled job and if possible, what would the syntax look like?
 
SELECT CreatedDate,CronExpression,CronJobDetailId,Id,NextFireTime,OwnerId.Name,PreviousFireTime,StartTime,State,TimesTriggered FROM CronTrigger

 
Best Answer chosen by HNT_Neo
Raj VakatiRaj Vakati
Refer this link

https://help.salesforce.com/articleView?id=000004423&language=en_US&type=1

All Answers

Raj VakatiRaj Vakati
Use this  and change the Job name from test to your job name
 
List<CronTrigger> lstCron = [select Id, State, NextFireTime, CronJobDetailId from CronTrigger where CronJobDetail.Name = 'test'];

 
HNT_NeoHNT_Neo
Hi Raj, I ran the code through the Developer Console but was unable to execute due to this: 
 User-added image
Raj VakatiRaj Vakati
Try this code
 
List<CronTrigger> listCronTrigger = [select Id, State, NextFireTime, 
CronJobDetailId from CronTrigger where CronJobDetail.Name = 'test' AND
 State = 'Waiting' or State='Running'];
		
         
System.debug('No of jobs: '+listCronTrigger.size());

If (listCronTrigger.size() > 0)
{
    for (Integer i = 0; i < listCronTrigger.size(); i++)
    { 
        System.abortJob(listCronTrigger[i].Id);
        System.debug('Job details ::'+String.valueOf(listCronTrigger[i]));
    }
}

 
Raj VakatiRaj Vakati
Refer this link

https://help.salesforce.com/articleView?id=000004423&language=en_US&type=1
This was selected as the best answer
HNT_NeoHNT_Neo
Hi Raj, thank you for this. I ran this and this error came up: 

User-added image
HNT_NeoHNT_Neo
Raj, I did try the apex code in the salesforce knowledge article and it worked. As you know, it, unfortunately, disables all running apex jobs. Salesforce definitely needs to fix that. Since this was in the sandbox, I am fine since it got me passed not being able to update this apex class. 
Thank you very much!