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

**URL:** <https://forum.buildkite.community/t/is-it-possible-to-cancel-builds-automatically-that-have-been-running-for-an-extended-period/2397>\
**Category:** Pipelines\
**Created:** [November 17, 2022, 5:25am UTC](https://forum.buildkite.community/t/is-it-possible-to-cancel-builds-automatically-that-have-been-running-for-an-extended-period/2397 "2022-11-17T05:25:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![verogo](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@verogo](https://forum.buildkite.community/u/verogo)\
**Post date:** [November 17, 2022, 5:25am UTC](https://forum.buildkite.community/t/is-it-possible-to-cancel-builds-automatically-that-have-been-running-for-an-extended-period/2397/1 "2022-11-17T05:25:32Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![paula](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/paula/32/954_2.png) [@paula](https://forum.buildkite.community/u/paula)\
**Post date:** [November 17, 2022, 12:18pm UTC](https://forum.buildkite.community/t/is-it-possible-to-cancel-builds-automatically-that-have-been-running-for-an-extended-period/2397/2 "2022-11-17T12:18:04Z")

</div>

Hey @verogo!

Welcome to the community! 🤗

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:

```auto
- 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](https://buildkite.com/docs/pipelines/skipping#cancel-running-intermediate-builds)) enabled and it should cancel all builds (except for the last one)

Best!

---

<div class="post-metadata">

**Author:** ![verogo](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@verogo](https://forum.buildkite.community/u/verogo)\
**Post date:** [November 18, 2022, 6:24am UTC](https://forum.buildkite.community/t/is-it-possible-to-cancel-builds-automatically-that-have-been-running-for-an-extended-period/2397/3 "2022-11-18T06:24:51Z")

</div>

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:

```auto
#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:

```auto
# 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.

🙂 :buildkite:

---

<div class="post-metadata">

**Author:** ![paula](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/paula/32/954_2.png) [@paula](https://forum.buildkite.community/u/paula)\
**Post date:** [August 29, 2023, 5:39am UTC](https://forum.buildkite.community/t/is-it-possible-to-cancel-builds-automatically-that-have-been-running-for-an-extended-period/2397/4 "2023-08-29T05:39:29Z")

</div>


