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

Last updated: 2020-01-28

I want to cancel or terminate all the AWS Batch jobs in my job queue at the same time.

Short Description

From the AWS Management Console, you can't cancel or terminate multiple jobs from a job queue all at once.

Note: The following resolution is for Linux or Mac OS environments.

Resolution

Before you complete the following steps, install the AWS Command Line Interface (AWS CLI) and JQ.

1.    In the AWS CLI, list the jobs in your job queue that are in the RUNNABLE state:

$aws batch list-jobs --job-queue myJOBQueue --job-status RUNNABLE | jq '.jobSummaryList[].jobId'

Note: Replace myJOBQueue with your job queue.

If you have three jobs stuck in the RUNNABLE state, then the command output looks similar to the following:

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

2.    Create a Bash script to cancel or terminate your jobs. See the following examples.

Script to cancel jobs:

#! /bin/bash
for i in $(aws batch list-jobs --job-queue myJOBQueue --job-status runnable | jq -r '.jobSummaryList[].jobId') 
do 
  echo "Cancel Job: $i"
  aws batch cancel-job --job-id $i --reason "Cancelling job."
  echo "Job $i deleted" 
done

Script to terminate jobs:

#! /bin/bash
for i in $(aws batch list-jobs --job-queue myJOBQueue --job-status runnable | jq -r '.jobSummaryList[].jobId') 
do 
  echo "Deleting Job: $i"
  aws batch terminate-job --job-id $i --reason "Terminating job."
  echo "Job $i deleted" 
done

Did this article help you?

Anything we could improve?


Need more help?