# Is there any way to programmatically cancel a workflow without making it "Failed" status?

**URL:** <https://discuss.circleci.com/t/is-there-any-way-to-programmatically-cancel-a-workflow-without-making-it-failed-status/38798>\
**Category:** Build Environment\
**Created:** [January 15, 2021, 8:09am UTC](https://discuss.circleci.com/t/is-there-any-way-to-programmatically-cancel-a-workflow-without-making-it-failed-status/38798 "2021-01-15T08:09:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![flex-heejong](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/flex-heejong/32/26453_2.png) [@flex-heejong](https://discuss.circleci.com/u/flex-heejong)\
**Post date:** [January 15, 2021, 8:09am UTC](https://discuss.circleci.com/t/is-there-any-way-to-programmatically-cancel-a-workflow-without-making-it-failed-status/38798/1 "2021-01-15T08:09:28Z")

</div>

Hi, I’m trying to achieve similar effect from [this question](https://discuss.circleci.com/t/how-to-programmatically-end-or-cancel-the-workflow-build/37742). My `run` command looks like below:

```auto
COUNT=$(git diff --name-only HEAD HEAD~1 | grep ^mobile/ | wc -l | xargs )
echo 'Changed file count from mobile directory:'
echo $COUNT

if [$COUNT == 0]; then
  echo 'No change from mobile directory: end build'
  curl --request POST \
    --url https://circleci.com/api/v2/workflow/$CIRCLE_WORKFLOW_ID/cancel \
    --header "Circle-Token: ${CIRCLE_TOKEN}"
  exit 0
fi

```

While it does stop further running of workflow, the call of suggested API (or using `circleci-agent step halt`) turns workflow status into ‘Failed’, not ‘Cancelled’, with output:

```auto
Exited with code exit status 1
CircleCI received exit code 1

```

I’d be much happier if there’s some way to make it ‘Cancelled’, so that I don’t have to manually check if ‘Failed’ build has actually failed due to a real problem, or it’s simply just skipped build.

Is there any way that I could cancel the workflow within itself, and make it ‘Cancelled’ status?

---

<div class="post-metadata">

**Author:** ![flex-heejong](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/flex-heejong/32/26453_2.png) [@flex-heejong](https://discuss.circleci.com/u/flex-heejong)\
**Post date:** [January 15, 2021, 8:39am UTC](https://discuss.circleci.com/t/is-there-any-way-to-programmatically-cancel-a-workflow-without-making-it-failed-status/38798/2 "2021-01-15T08:39:01Z")

</div>

Well never mind, I just found out that my bash script is what’s causing the exit code to be non-zero… What a silly mistake 😭

---

<div class="post-metadata">

**Author:** ![henrik](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/henrik/32/21681_2.png) [@henrik](https://discuss.circleci.com/u/henrik)\
**Post date:** [September 20, 2021, 1:18pm UTC](https://discuss.circleci.com/t/is-there-any-way-to-programmatically-cancel-a-workflow-without-making-it-failed-status/38798/3 "2021-09-20T13:18:43Z")

</div>

I ran into the same situation and this is what I did:

```auto
curl …
sleep 30 # Give cancellation a chance to run. This script then won't continue running.
exit 1 # If cancellation didn't run for some reason, fail the build.

```

I also want to clarify that whereas the CIRCLE\_WORKFLOW\_ID is provided for you automatically, CIRCLE\_TOKEN isn’t. So you need to generate such a token and then add it to your environment variables config.
