+ Start a Discussion
HNT_NeoHNT_Neo 

How to cancel an apex batch job

Hello, 
Trying to cancel an apex batch job but no luck making it work. 

This is the apex code I'm using and the error results are below. Any help is greatly appreciated!
 
string SCHEDULE_NAME = 'MergeMetricsCalculationBatchable';
System.abortJob(SCHEDULE_NAME);
 
Execute anonumous error

Line: 2, Column: 1
System.StringException: Only CronTrigger IDs and batch, future, and queueable job IDs are supported.

 
Best Answer chosen by HNT_Neo
Raj VakatiRaj Vakati
You need to pass the job Id 


 Run the following query to find the scheduled or future Apex job id to delete:
 
SELECT ApexClassId,CompletedDate,CreatedById,CreatedDate,ExtendedStatus,Id,JobItemsProcessed,JobType,LastProcessed,LastProcessedOffset,MethodName,NumberOfErrors,ParentJobId,Status,TotalJobItems FROM AsyncApexJob where status = 'queued'

.Pass the job Id here 
 
System.abortJob('JobID');
Refer this link 

https://help.salesforce.com/articleView?id=000003873&type=1

OR 

Go to Setup>Monitor>Jobs>Scheduled Jobs --> Delete from here also 


 

All Answers

Raj VakatiRaj Vakati
You need to pass the job Id 


 Run the following query to find the scheduled or future Apex job id to delete:
 
SELECT ApexClassId,CompletedDate,CreatedById,CreatedDate,ExtendedStatus,Id,JobItemsProcessed,JobType,LastProcessed,LastProcessedOffset,MethodName,NumberOfErrors,ParentJobId,Status,TotalJobItems FROM AsyncApexJob where status = 'queued'

.Pass the job Id here 
 
System.abortJob('JobID');
Refer this link 

https://help.salesforce.com/articleView?id=000003873&type=1

OR 

Go to Setup>Monitor>Jobs>Scheduled Jobs --> Delete from here also 


 
This was selected as the best answer
HNT_NeoHNT_Neo
Raj, apparently I had already deleted it, however, this is a good reference for all to see. Thank you!
Ajay K DubediAjay K Dubedi
Hi JH_Neo,
You can go with this code:
public class AbortScheduledJob {
    public static void abortScheduledJob () {
        CronTrigger abort_job = [SELECT Id FROM CronTrigger WHERE CronJobDetail.Name = : 'ScheduleClass' limit 1];
            System.abortJob(abort_job.Id); //abort the job
        }   
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
job alertjob alert
Okay prefer to land all forthcoming Govt Positions 2019? Fresherslive is here to give snappy updates of most recent Govt Jobs right away. Free notice of up and coming Government Jobs in India 2019 warning buy govt jobs (https://www.highonstudy.com/government-jobs) in to our page and discover recently reported All Govt Jobs October crosswise over India first on Fresherslive refreshed on 31-10-2019.