Available on: 0.21.0

Set a block of tasks to run at the end of a Flow regardless of task status.

finally tasks are beneficial for running cleanup operations at the end of your flow regardless of whether the end state was a success or failure.

finally component

finally is a block of tasks executed at the end of your workflow. They run irrespective of the status of the prior tasks. finally enables you to ensure that any necessary cleanup operations occur at the end of a workflow without considering the state of other tasks.

For example, you may want to have the finally task turn off a cloud service when the flow is finished, regardless of whether it was successful or not.

finally example

In the example below, a task is programmed to fail, and an error task logs a message as an alert communicating the failure. The finally task runs after the other tasks have finished regardless of the failure, in this case logging another message, but could alternatively be used to shut down any resources specifically spun up to use in the flow.

yaml

id: finally_example
namespace: company.team

tasks:
  - id: fail
    type: io.kestra.plugin.core.execution.Fail
    errorMessage: Test downstream tasks

errors:
  - id: send_alert
    type: io.kestra.plugin.core.log.Log
    message: alert on failure

finally:
- id: cleanup_task
  type: io.kestra.plugin.core.log.Log
  message: cleaning up resources

Change the example to ensure the end state of the first task is a success, like below, and you can see that the finally task runs the same as before:

yaml

id: finally_example
namespace: company.team

tasks:
  - id: log
    type: io.kestra.plugin.core.log.Log
    errorMessage: "This flow executes successfully!"

errors:
  - id: send_alert
    type: io.kestra.plugin.core.log.Log
    message: alert on failure

finally:
- id: cleanup_task
  type: io.kestra.plugin.core.log.Log
  message: cleaning up resources

Like in the first iteration of the flow, the finally task runs at the end despite the errors task not sending an alert, ensuring any cleanup operations still take place regardless of status.

Was this page helpful?