<@U06RKAMK0UC> Is there any way to run a cleanup t...
# ask-ai
a
@ancient-wolf-19325 Is there any way to run a cleanup task on abort?
a
Flyte does not provide a built-in mechanism to automatically run a specific "cleanup task" on abort. However, you can handle cleanup logic within your task code by catching cancellation or abort exceptions. For example, if a task is cancelled (e.g., due to workflow abort), it receives an
asyncio.CancelledError
(for programmatic cancellation) or a
flyte.errors.ActionAbortedError
(for external aborts), and you can place cleanup code in the corresponding exception handler. There is no explicit support for registering a separate cleanup task to be triggered on abort outside of this pattern, according to the documentation and examples provided in the data sources. Here is a pattern for handling cleanup on abort within a task:
Copy code
import asyncio
import flyte
import flyte.errors

@flyte.task
async def my_task():
    try:
        # main logic here
        await asyncio.sleep(100)
    except (asyncio.CancelledError, flyte.errors.ActionAbortedError):
        # cleanup logic here
        print("Cleanup on abort")
        return
As per the documentation, this is the recommended approach for cleanup on aborts in Flyte tasks. Would you like more detail or a more advanced example? Sources: - Abort and cancel actions - Abort and cancel actions (BYOC) - llms-full.txt - Code example: abort-tasks - Code example: external abort 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
a
what about in Flyte V1
@ancient-wolf-19325 what about in Flyte V1
a
The provided data sources do not contain explicit documentation or examples for running a cleanup task on abort in Flyte V1. All referenced mechanisms for handling aborts and cleanup (such as catching
asyncio.CancelledError
or
ActionAbortedError
within task code) are documented for Flyte V2 only. There is no mention of a built-in or recommended cleanup-on-abort pattern for Flyte V1 in the available sources. Would you like more detail or guidance on possible workarounds based on general Python or Kubernetes patterns? Sources: - Abort and cancel actions - Abort and cancel actions (BYOC) 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.