Is it possible to cancel builds automatically that have been running for an extended period?

Hi Buildkite team :)!

I have more than 100 builds running (waiting for an event that will not happen), and I would like to know if there is a way to stop/cancel them automatically instead of going one by one and doing it.

Thank you very much!

1 Like

Hey @verogo!

Welcome to the community! :hugs:

We don’t have a bulk cancelling at the moment, so the best option is to script our api. For example with REST API, you can have something like:

- export API_TOKEN=xxxxxxxx (export your API Token)
- curl --silent -H 'Authorization: Bearer $API_TOKEN' 'https://api.buildkite.com/v2/builds?state=scheduled' | jq '.[] | .url + "/cancel"' -r | while read -r URL; do echo "$URL to delete"; done

If they are all in the same pipeline, you can have the option Cancel Intermediate Builds (Skipping Builds | Buildkite Documentation) enabled and it should cancel all builds (except for the last one)

Best!

1 Like

Thank you very much, @paula for your guidance.

I ended up making a python program to cancel the “running for ever builds” (there were not in the schedule state).

So, I used Graphql to make that happens, and the main queries were:

#Get the list of the builds created before the given time (too days ago).
query GetRunningBuilds {
  pipeline(slug:"organization-slug/pipeline-slug/") {
    name
    builds(first:200, state: RUNNING, createdAtTo: "2022-11-16T10:25:34.790Z"){
      edges{
        node{
          id
          number
          state
          createdAt
          finishedAt
          url
        }
      }
      count
    }
  }
}

And then canceled them:

# Cancel builds and get some audit data.
mutation buildPipelineCancel ($id: ID!) {
  buildCancel(input:{ id: $id } ){
    build{
      number
      createdAt
      canceledAt
      canceledBy
      finishedAt
      url
    }
  }
}

Therefore, it is all sorted now.

On the flip side, thank you very much for the documentation as well.
I will have a look at it, and if I have any further question, I will ping you again.

:slight_smile: :buildkite:

1 Like