How do I cancel all RUNNABLE jobs from my job queue in AWS Batch?

Last updated: 2021-07-21

I want to cancel all the AWS Batch jobs in my job queue that are in the RUNNABLE state. How do I do that?

Short description

The AWS Management Console allows you to cancel or terminate only one AWS Batch job from a job queue at a time. To cancel or terminate more than one AWS Batch job from your job queue, you must use the AWS Command Line Interface (AWS CLI).

To cancel all the jobs in your job queue that are in the RUNNABLE state, use the following procedure.

Note: The following procedure can be used in Linux or macOS environments only.

Resolution

Note: If you receive errors when running AWS CLI commands, make sure that you’re using the most recent AWS CLI version.

1.    Return a list of all the jobs in your job queue that are in the RUNNABLE state by running the following list-jobs command:

Important: Replace myJOBQueue with your job queue's name.

$aws batch list-jobs --job-queue myJOBQueue --job-status runnable --output text --query jobSummaryList[*].[jobId]

The following is an example of a list-jobs command output that includes three jobs stuck in the RUNNABLE state:

----------------------------------------------------------------------------------------
"0cffddb0-8bfa-4ba4-86ba-c5cad59e4529"
"884d8bdf-6192-4d4c-ac85-c51093460a01"
"236670e3-127a-4fe1-afd6-3f21cf1fe02e"
----------------------------------------------------------------------------------------

2.    To cancel all the AWS Batch jobs in your job queue that are stuck in the RUNNABLE state, run the following bash script:

Important: Replace myJOBQueue with your job queue's name.

Cancel jobs example bash script

#! /bin/bash
for i in $(aws batch list-jobs --job-queue myJOBQueue --job-status runnable --output text --query jobSummaryList[*].[jobId])
do
  echo "Cancel Job: $i"
  aws batch cancel-job --job-id $i --reason "Cancelling job."
  echo "Job $i canceled"
done

Note: To terminate all the jobs from your job queue, including all RUNNING jobs, you can run the following bash script instead:

Terminate and cancel all AWS Batch jobs in a job queue bash script example

#! /bin/bash
for state in SUBMITTED PENDING RUNNABLE STARTING RUNNING
do 
    for job in $(aws batch list-jobs --job-queue myJOBQueue --job-status $state --output text --query jobSummaryList[*].[jobId])
    do 
        echo -ne "Stopping job $job in state $state\t"
        aws batch terminate-job --reason "Terminating job." --job-id $job && echo "Done." || echo "Failed."
    done
done

Important: Running this bash script cancels all the jobs in your queue that haven't progressed to the STARTING state.


Did this article help?


Do you need billing or technical support?