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

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

Short descriptionThe 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.

ResolutionUse the CancelJob API or TerminateJob API to cancel all jobs that are in the following states:SUBMITTEDPENDINGRUNNABLESTARTINGHowever, 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:Linuxexport 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 queueTo 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 jobidThe following example output includes three jobs in the RUNNABLE state:0cffddb0-8bfa-4ba4-86ba-c5cad59e4529884d8bdf-6192-4d4c-ac85-c51093460a01236670e3-127a-4fe1-afd6-3f21cf1fe02eCancel jobs with the CancelJob APITo 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"doneFor 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 APITo terminate all the jobs from your job queue, including all RUNNING jobs, run the following bash script:for state in SUBMITTED PENDING RUNNABLE STARTING RUNNINGdo     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    donedoneFor 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     }}