1
votes

In Azure DevOps pipelines, How do I cancel all pending jobs for a job pool. I've got lots queued and couldn't see where I can cancel all the jobs I have waiting.

1

1 Answers

1
votes

Azure devops doesnot yet have this feature to cancel all the pending jobs in batch from the UI partal.

You can write scripts to call rest api to cancel all the pending jobs as walkaround. Check out below steps:

First, use list build rest api to get all the pending jobs.

https://dev.azure.com/{organization}/{project}/_apis/build/builds?statusFilter=notStarted&api-version=5.1

Then, use update build api to cancel the pending jobs:

PATCH https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}?api-version=5.1

See below powershell scripts for reference:

Check here to get a Personal access token that will be used in below scripts.

$url= "https://dev.azure.com/{organization}/{project}/_apis/build/builds?statusFilter=notStarted&api-version=5.1"

$pat="Personal Access Token"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)"))

$pendingJobs=Invoke-RestMethod -Uri $url-Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} -Method get -ContentType "application/json" 

$jobsToCancel = $pendingJobs.value

#Pending jobs donot consume the job agents in the agent pool. To filter the definition name to cancel pending jobs for a particular pipeline, you can use below filter criteria. 
#$jobsToCancel = $pendingJobs.value | where {$_.definition.Name -eq "{Name of your pipeline }"}

#call update api to cancel each job.
ForEach($build in $jobsToCancel)
{
   $build.status = "Cancelling"
   $body = $build | ConvertTo-Json -Depth 10
   $urlToCancel = "https://dev.azure.com/{organization}/{project}/_apis/build/builds/$($build.id)?api-version=5.1"
   Invoke-RestMethod -Uri $urlToCancel -Method Patch -ContentType application/json -Body $body -Header @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
}

You can also submit a new feature request(Click Suggest a feature and choose azure devops) to Microsoft development team for supporting cancelling pending jobs in batch. Hopefully they will consider adding this feature in the future sprint.