function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SV MSV M 

Stop Batch Class

Hi, I have a batch class which was running continuously form a long time. Can someone tell me how to abort the job? I have tried aborting from Setup -> Apex Jobs but that didn't work. Is there any other way to abort the job. Please tell me if there is any another way...

Thanks in Advance...
Best Answer chosen by SV M
SwethaSwetha (Salesforce Developers) 
HI Sai,
What is the status of the apex job that you are trying to abort?

Can you run the below query and share the result
SELECT Id, CreatedDate,Status FROM AsyncApexJob WHERE JobType='ApexToken' AND Status = 'Queued' 

If the above query returns any records then please log a ticket with Salesforce Support to have the apextoken jobs cleared.

If there are no records returned in the above query then follow the below steps:
1. Login to "https://workbench.developerforce.com/login.php"
2. On right corner, it will be showing your name and API version. Click on the link.
3. There you will find change API version, change it to 31
4. Go to Utilities >> Apex Execute and run the command below.

for ( AsyncApexJob aJob : [ Select id ,Status from AsyncApexJob where Status = 'Queued' or Status='holding'] )
{
System.AbortJob(aJob.Id);
}

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

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful.

Thanks

All Answers

Danish HodaDanish Hoda
Hi Sai Vineeth,
Abort will stop the current execution of the Batch, but your Batch is being executed from a Schedule class, that need to be deleted from Scheduled Jobs.
SwethaSwetha (Salesforce Developers) 
HI Sai

To abort long running batch, future or scheduled Apex jobs, you may use System.abortJob() from the Developer Console (execute anonymous window) and pass the job id to this method.

Please follow the articles for more information
https://help.salesforce.com/articleView?id=000324656&type=1&mode=1

https://salesforce.stackexchange.com/questions/121630/abort-currently-running-job-in-salesforce

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful.

Thanks
SV MSV M
Hi, Swetha I've tried the code which you mentioned but my batch class was still running in the background. Can you help me how to stop the batch class from running continuously...
SwethaSwetha (Salesforce Developers) 
HI Sai,
What is the status of the apex job that you are trying to abort?

Can you run the below query and share the result
SELECT Id, CreatedDate,Status FROM AsyncApexJob WHERE JobType='ApexToken' AND Status = 'Queued' 

If the above query returns any records then please log a ticket with Salesforce Support to have the apextoken jobs cleared.

If there are no records returned in the above query then follow the below steps:
1. Login to "https://workbench.developerforce.com/login.php"
2. On right corner, it will be showing your name and API version. Click on the link.
3. There you will find change API version, change it to 31
4. Go to Utilities >> Apex Execute and run the command below.

for ( AsyncApexJob aJob : [ Select id ,Status from AsyncApexJob where Status = 'Queued' or Status='holding'] )
{
System.AbortJob(aJob.Id);
}

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

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful.

Thanks
This was selected as the best answer
SV MSV M
Hi, Swetha
The method worked and the batch was stopper successfully. Thanks for your help...
Naseem Akhtar 11Naseem Akhtar 11
Try this to abort the batch class.
7072w00009StBHW this is ApexJob Id

AsyncApexJob ct =
    [SELECT Id,ParentJobId,cronTriggerId
    FROM AsyncApexJob WHERE Id ='7072w00009StBHW'];
     System.abortJob(ct.cronTriggerId);