# How to define cleanup for workflow cancellations (typescript-sdk)

**URL:** <https://community.temporal.io/t/how-to-define-cleanup-for-workflow-cancellations-typescript-sdk/6840>\
**Category:** Community Support\
**Tags:** activity, typescript-sdk\
**Created:** [January 4, 2023, 8:42am UTC](https://community.temporal.io/t/how-to-define-cleanup-for-workflow-cancellations-typescript-sdk/6840 "2023-01-04T08:42:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Llama\_D\_Attore](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/llama_d_attore/32/2890_2.png) [@Llama\_D\_Attore](https://community.temporal.io/u/Llama_D_Attore)\
**Post date:** [January 4, 2023, 8:42am UTC](https://community.temporal.io/t/how-to-define-cleanup-for-workflow-cancellations-typescript-sdk/6840/1 "2023-01-04T08:42:09Z")

</div>

#### issue

There are some Workflows we intentionally cancel mid-execution when we detect additional work is not needed. Normally at the end of these Workflows we run a cleanup activity to get rid of tmp files created by the workflow. However, our cleanup activity is never run when Workflows are cancelled mid-exec. Is there a way to configure this activity to also be run when a workflow is cancelled?

#### background info

If it makes any difference, Cancellation is triggered from within an activity with a slightly more graceful version of:

```auto
let workflowId = Context.current().info.workflowExecution.workflowId
let handle = wfClient.getHandle(workflowId);
await handle.cancel();

```

Looked at the [typescript-sdk-docs on failure handling](https://legacy-documentation-sdks.temporal.io/typescript/handling-failure) and the part on cancelled-failure links to a 404.

---

<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:** [January 4, 2023, 12:42pm UTC](https://community.temporal.io/t/how-to-define-cleanup-for-workflow-cancellations-typescript-sdk/6840/2 "2023-01-04T12:42:48Z")

</div>

Helle @Llama_D_Attore

Cancellation is delivered to your activities through heartbeat, do your activities heartbeat?

if so, in your workflow code you should be able to catch the CancelledFailure error thrown by your activity when it is cancelled and run the cleanup activity.

Can you paste here the text that contains the broken link, I am not able to find it.

Thank you.

---

<div class="post-metadata">

**Author:** ![bergundy](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/bergundy/32/900_2.png) [@bergundy](https://community.temporal.io/u/bergundy)\
**Post date:** [January 4, 2023, 6:11pm UTC](https://community.temporal.io/t/how-to-define-cleanup-for-workflow-cancellations-typescript-sdk/6840/3 "2023-01-04T18:11:07Z")

</div>

To ensure the activity will be run, it should be executed in a non cancellable cancellation scope.  
See this example for reference: [Cancellation Scopes | Legacy documentation for Temporal SDKs](https://legacy-documentation-sdks.temporal.io/typescript/cancellation-scopes#nesting-cancellation-scopes)
