# Recommended way for running cleanup activity on workflow timeout

**URL:** <https://community.temporal.io/t/recommended-way-for-running-cleanup-activity-on-workflow-timeout/6455>\
**Category:** Community Support\
**Tags:** go-sdk\
**Created:** [November 11, 2022, 12:42am UTC](https://community.temporal.io/t/recommended-way-for-running-cleanup-activity-on-workflow-timeout/6455 "2022-11-11T00:42:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AhmedMozaly](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/ahmedmozaly/32/2837_2.png) [@AhmedMozaly](https://community.temporal.io/u/AhmedMozaly)\
**Post date:** [November 11, 2022, 12:42am UTC](https://community.temporal.io/t/recommended-way-for-running-cleanup-activity-on-workflow-timeout/6455/1 "2022-11-11T00:42:40Z")

</div>

We have a workflow that uses session to run all activities on one worker, one of these activities creates a temp directory that needs to be cleaned up even if the workflow fails for any reason (and to communicate to the user that workflow fails)

We use `WorkflowExecutionTimeout` to set the max time duration for the workflow

The question is, what is the recommended way to run a cleanup activity (and report failure to the user) if workflow fails for ANY reason?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [November 11, 2022, 6:51pm UTC](https://community.temporal.io/t/recommended-way-for-running-cleanup-activity-on-workflow-timeout/6455/2 "2022-11-11T18:51:22Z")

</div>

The recommended way is not to use _terminate_ (which is like kill -9) and workflow timeout, which is the terminate on a timer.

For any business logic timeouts, use timers inside the workflow code.

---

<div class="post-metadata">

**Author:** ![AhmedMozaly](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/ahmedmozaly/32/2837_2.png) [@AhmedMozaly](https://community.temporal.io/u/AhmedMozaly)\
**Post date:** [November 13, 2022, 12:31am UTC](https://community.temporal.io/t/recommended-way-for-running-cleanup-activity-on-workflow-timeout/6455/3 "2022-11-13T00:31:37Z")

</div>

Thanks for the info.  
I assume that this termination is done on server, Does this also sends anything to the worker? (cancellation request or signal)

If so, is there some way to listen of this “termination” request? using interceptor for example

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [November 13, 2022, 12:52am UTC](https://community.temporal.io/t/recommended-way-for-running-cleanup-activity-on-workflow-timeout/6455/4 "2022-11-13T00:52:16Z")

</div>

No. The whole idea of termination is that it is a _hard kill_ that terminates a workflow without any chance of cleanup. If you need to perform cleanup, use workflow cancellation. Or if you need to perform cleanup after some time use timers inside the workflow code.

---

<div class="post-metadata">

**Author:** ![AhmedMozaly](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/ahmedmozaly/32/2837_2.png) [@AhmedMozaly](https://community.temporal.io/u/AhmedMozaly)\
**Post date:** [March 28, 2023, 9:08am UTC](https://community.temporal.io/t/recommended-way-for-running-cleanup-activity-on-workflow-timeout/6455/5 "2023-03-28T09:08:21Z")

</div>

We solved this issue by using a parent workflow which runs the target workflow as “Child Workflow”  
This enabled us to get the child workflow timeout error and handle it properly
