Development, Salesforce

Mass abort Queued APEX Jobs

I ran into an issue the other day while trying to deploy to an org with a lot of ongoing activity. I ran into the annoying error message:

Error: Apex class has Batchable or Future jobs pending or in progress
 (line 1, column 21)

I checked the Apex Jobs queue and found that there was over 5500 queued jobs. Some which had been waiting for hours. I decided to clear the queue so that I could continue with the deployment. I wrote a quick APEX script to mass abort all the queued jobs which I then executed using annonymous APEX.

When working with the Apex Jobs queue I believe it’s quicker to review it using a DB-tool (like force.com IDE – explore, Force.com explorer or why not force CLI). This way you can simply filter and sort to get to what you need without needing to configure any views.

Do you like the content on this page? Sign up for our newsletter
to make sure you are not missing out!

The snipped I used can be found below – the two first lines are there to be used instead of line three if it is only jobs from one specific class I want to mass remove from the queue.

//ApexClass apxClass = [SELECT Id FROM ApexClass WHERE Name = 'myScriptName'];
//List<AsyncApexJob> lstJobs = [SELECT Id FROM AsyncApexJob WHERE Status = 'Queued' AND ApexClassId = :apxClass.Id];
List<AsyncApexJob> lstJobs = [SELECT Id FROM AsyncApexJob WHERE Status = 'Queued'];

for(AsyncApexJob job : lstJobs) {
   try {
       System.abortJob(job.Id);
   } catch(Exception ex ) {
        System.debug(ex);
    }
}

This blog post is a repost from Martin’s old blog called webaholic.

Leave a Reply

Your email address will not be published. Required fields are marked *