Loading

Abort Apex test class execution using ApexTestQueueItems

Julkaisupäivä: Jun 2, 2026
Kuvaus

If an Apex test class is stuck in the Salesforce job execution queue and you receive the error:
Unable to invoke async test job: This test is already in the execution queue.
You need to abort the queued ApexTestQueueItem records before you can re-run the test.
ApexTestQueueItem is the Salesforce object that represents Apex test jobs in the asynchronous job queue. When tests are queued but not yet completed, their status can be set to "Aborted" to cancel execution. You can do this by running an Anonymous Apex script in the Developer Console.
Per the Salesforce documentation: "To abort a class that is in the Apex job queue, perform an update operation on the ApexTestQueueItem object and set its Status field to Aborted."

Ratkaisu

Steps to Abort Queued Apex Tests

Run the following Apex code as Anonymous Apex in the Developer Console (Help > Developer Console > Debug > Open Execute Anonymous Window):

List<ApexTestQueueItem> items = [Select Id,ApexClassId,Status,ExtendedStatus,ParentJobId from ApexTestQueueItem where Status != 'Completed'];
for(ApexTestQueueItem atqi : items) {
    atqi.Status = 'ABORTED';
}
update items;


This code does the following:

  1. Queries all ApexTestQueueItem records that have not yet reached a Completed status. This includes items with a status of Queued, Processing, or Failed.
  2. Updates each item's Status to "ABORTED," marking them for cancellation.
  3. Performs a DML update to save the changes to Salesforce.

After running this code, navigate to Setup > Apex Jobs and refresh the page to confirm the ApexTestQueueItem records have been aborted. You can then re-run your test classes without encountering the "already in the execution queue" error.

Note

This script aborts all non-completed queued test items in the org. If you only want to abort specific tests, add a filter to the SOQL query, for example by specifying the ApexClassId of the test class you want to abort.

Knowledge-artikkelin numero

000384395

 
Ladataan
Salesforce Help | Article