# How to cancel an activity?

**URL:** <https://community.temporal.io/t/how-to-cancel-an-activity/5344>\
**Category:** Community Support\
**Tags:** go-sdk, general-impl\
**Created:** [July 15, 2022, 3:36am UTC](https://community.temporal.io/t/how-to-cancel-an-activity/5344 "2022-07-15T03:36:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![haojie](https://avatars.discourse-cdn.com/v4/letter/h/53a042/32.png) [@haojie](https://community.temporal.io/u/haojie)\
**Post date:** [July 15, 2022, 3:36am UTC](https://community.temporal.io/t/how-to-cancel-an-activity/5344/1 "2022-07-15T03:36:30Z")

</div>

I know it can cancel executing activity by cancelWorkflow API , my question is to cancel a specific activity by signal or something else, instead of cancelling a specific activity via the parent workflow. Are there some examples that I can learn from.

---

<div class="post-metadata">

**Author:** ![antonio.perez](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/antonio.perez/32/2417_2.png) [@antonio.perez](https://community.temporal.io/u/antonio.perez)\
**Post date:** [July 15, 2022, 12:15pm UTC](https://community.temporal.io/t/how-to-cancel-an-activity/5344/3 "2022-07-15T12:15:52Z")

</div>

Hi @haojie

One thing you can do is to create a signalMethod in the parentWorkflow and cancel the activity from there, see this example [temporal-cancel-activity/TemporalWorkflow.java at main · tsurdilo/temporal-cancel-activity · GitHub](https://github.com/tsurdilo/temporal-cancel-activity/blob/main/src/main/java/io/temporaldemo/TemporalWorkflow.java#L49)

I hope this helps.

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [July 15, 2022, 1:36pm UTC](https://community.temporal.io/t/how-to-cancel-an-activity/5344/4 "2022-07-15T13:36:57Z")

</div>

> my question is to cancel a specific activity by signal

Just to add, for Go you could use `workflow.WithCancel` (see sample [https://github.com/temporalio/samples-go/blob/main/pickfirst/pickfirst\_workflow.go](https://github.com/temporalio/samples-go/blob/main/pickfirst/pickfirst_workflow.go)):

`activityCtx, cancelActivity := workflow.WithCancel(ctx)`

and invoke your activity that can be cancelled with `activityCtx`. When you receive a signal call  
`cancelActivity()` to cancel. Note your activity has to heartbeat to receive cancellation requests (will be receive on next heartbeat). You can also set ActivityOptions-\>WaitForCancellation to true if you need to you wait for canceled activity to be completed.
