Apache Airflow / Intermediate: runtime work, control flow and resource budgets / Lesson 14
Setup, teardown and resource lifecycle
Plan temporary-resource ownership and cleanup using Airflow setup and teardown task semantics.
On this page
Lesson goals
- Explain how setup and teardown relate to work task outcomes.
- Test cleanup and DAG-run reporting without touching real infrastructure.
Define the temporary resource
A workflow may create an isolated compute resource, use it for a bounded task and then remove it. Define the resource identity, owner, lifetime and cleanup authorization before designing the DAG. Setup and teardown tasks express this relationship in Airflow, but a task state alone cannot prove that an external resource was actually removed. Keep an independent inventory and recovery procedure for uncertain cleanup.
Consider work failure
The first synthetic scenario has a successful setup followed by failed work. Under the selected Airflow 3.1 setup and teardown semantics, a correctly associated teardown can run after its upstream work finishes even when that work failed. The JSON is an expected state relationship, not a deployed DAG or a claim that a cloud resource was created or deleted.
{"setup":"success","work":"failed","teardown_expected":"run"}
The example expects associated cleanup to run; actual task and resource outcomes still require verification.
Consider skipped setup
In the second scenario the setup is skipped, so the associated teardown is skipped under the documented relationship. Do not generalize this simple case to every graph: multiple setups, dependencies and task groups have specific semantics. Review the exact DAG and Airflow version, and ensure the work cannot use a resource whose setup did not succeed.
{"setup":"skipped","work":"skipped","teardown_expected":"skipped"}
No successful setup is present in this simple fixture, so its teardown is expected to skip.
Separate cleanup from run status
In the selected Airflow documentation, teardown tasks are ignored for DAG-run state by default, with an option to make teardown failure affect that state. Choose the behavior deliberately and monitor cleanup independently when required. A green DAG run can therefore coexist with failed cleanup under default behavior. Verify the actual configuration and external resource state rather than using a single run color as an inventory.
Exercise cleanup safely
Use a disposable local fixture representing resource creation and deletion, not a real cloud account. Test successful work, failed work and skipped setup, then simulate a cleanup failure and inspect the chosen DAG-run behavior. Verify the resource marker and ownership evidence after each path. Document how an operator finds and reconciles an uncertain resource if the scheduler or external service becomes unavailable.
Common errors and fixes
A temporary resource remains after failed work.
- Why it happens
- Cleanup was treated as an ordinary success-only downstream task.
- How to fix it
- Model and test the intended setup and teardown relationship.
- Prevent it next time
- Include work-failure cleanup in the DAG review.
A green DAG run is assumed to prove cleanup succeeded.
- Why it happens
- Default teardown run-state behavior was overlooked.
- How to fix it
- Inspect teardown and external resource state.
- Prevent it next time
- Choose run-state behavior and cleanup monitoring explicitly.
A cleanup task deletes another runβs resource.
- Why it happens
- Resource identity and ownership were not scoped.
- How to fix it
- Stop and reconcile the resource with its owner.
- Prevent it next time
- Use run-specific identifiers and authorized bounded cleanup.
2 practice exercises
1. Setup succeeds and work fails. What should the associated teardown do in the documented simple case?
Show worked solution
It can run after the upstream work is finished because the setup succeeded. Verify its actual result and the external resource separately; the state relationship alone does not prove deletion.
2. Why can a successful DAG-run state be insufficient cleanup evidence?
Show worked solution
The selected Airflow behavior ignores teardown tasks for DAG-run state by default. Inspect the teardown outcome, chosen configuration and external resource inventory rather than relying on the run state alone.
Key takeaways
- Model resource ownership and setup/teardown dependencies explicitly.
- Verify cleanup and run-state behavior as separate evidence.