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

Lesedauer: 3 Minute
1

I want to cancel or terminate all the AWS Batch jobs in my job queue that are in the RUNNABLE state.

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 multiple AWS Batch jobs from your queue, use the AWS Command Line Interface (AWS CLI) or AWS Tools for PowerShell.

To cancel all the jobs in your job queue that are in the RUNNABLE state, use either the CancelJob API or TerminateJob API.

Resolution

Use the CancelJob API or TerminateJob API to cancel all jobs that are in the following states:

  • SUBMITTED
  • PENDING
  • RUNNABLE
  • STARTING

However, TerminateJob also cancels all jobs that are in the RUNNING state. Use the API that's best suited for your use case. For more information, see TerminateJob and CancelJob.

Before you run either of these commands, set the $myJOBQueue environment variable to your job queue's name:

Linux

export myJOBQueue="job-queue-name"

PowerShell

$myJOBQueue="job-queue-name"

Note: If you receive errors when running AWS CLI commands, make sure that you're using the most recent AWS CLI version or the AWS Tools for PowerShell.

(Optional) See all RUNNABLE jobs in your queue

To return a list of all the jobs in your job queue that are in the RUNNABLE state, run the following list-jobs command:

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

For PowerShell, use the following command:

Get-BATJobList -JobQueue $myJOBQueue  -JobStatus RUNNABLE | select -ExpandProperty jobid

The following example output includes three jobs in the RUNNABLE state:

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

Cancel jobs with the CancelJob API

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

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

For PowerShell, use the following script:

Foreach ($I in Get-BATJobList -JobQueue $myJOBQueue -JobStatus RUNNABLE | select -ExpandProperty jobid) { 
    echo "Cancel job $I"
    Stop-BATJob -JobId $I -Reason "Cancelling job." 
}

Terminate jobs with the TerminateJob API

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

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 "Stopping job $job in state $state"
        aws batch terminate-job --reason "Terminating job." --job-id $job
    done
done

For PowerShell, run the following script:

Foreach ($STATE in "SUBMITTED","PENDING","RUNNABLE","STARTING","RUNNING") {
    Foreach ($I in Get-BATJobList -JobQueue $myJOBQueue -JobStatus $STATE | select -ExpandProperty jobid) {
        echo "Stopping job $I in state $STATE"
        Remove-BATJob -JobId $I -Reason "Terminating job." -Force 
    }
}
AWS OFFICIAL
AWS OFFICIALAktualisiert vor einem Jahr