# Doing cleanup when using Sessions

**URL:** <https://community.temporal.io/t/doing-cleanup-when-using-sessions/193>\
**Category:** Community Support\
**Tags:** go-sdk\
**Created:** [July 15, 2020, 6:01pm UTC](https://community.temporal.io/t/doing-cleanup-when-using-sessions/193 "2020-07-15T18:01:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nkatsaros](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/nkatsaros/32/125_2.png) [@nkatsaros](https://community.temporal.io/u/nkatsaros)\
**Post date:** [July 15, 2020, 6:01pm UTC](https://community.temporal.io/t/doing-cleanup-when-using-sessions/193/1 "2020-07-15T18:01:01Z")

</div>

If I create a temporary directory on a worker using the Session API (think the file processing example but everything is going in a temporary directory), how can I guarantee deleting the temporary directory when the workflow succeeds/fails/gets cancelled?

I see on [https://community.temporal.io/t/go-defer-executing-a-last-activity/74](https://community.temporal.io/t/go-defer-executing-a-last-activity/74) that I can use `defer` to run a workflow, but if I use the disconnected context will it run on the same worker that the session was started on?

---

<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:** [July 15, 2020, 6:10pm UTC](https://community.temporal.io/t/doing-cleanup-when-using-sessions/193/2 "2020-07-15T18:10:23Z")

</div>

I think in this case you want to run the whole session in a disconnected context and use some other mechanism to notify activities in the session that cancellation has happened.

I filed [an issue](https://github.com/temporalio/go-sdk/issues/194) to come up with cleaner design.

---

<div class="post-metadata">

**Author:** ![ryland](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/ryland/32/11_2.png) [@ryland](https://community.temporal.io/u/ryland)\
**Post date:** [July 15, 2020, 6:11pm UTC](https://community.temporal.io/t/doing-cleanup-when-using-sessions/193/3 "2020-07-15T18:11:53Z")

</div>

The most straightforward way I know of is by creating a host specific taskqueue on each worker and then passing that to the context when you invoke the cleanup activity. There is a decent example in the Java version of the file processing code:

> <https://github.com/temporalio/java-samples/blob/76713f24490a0cec2a0ead458f3c5a4a0a56bf83/src/main/java/io/temporal/samples/fileprocessing/FileProcessingWorkflowImpl.java#L67>

Using the per-host taskqueue makes this possible.
