Skip to content
Silver 5 min read · Scale 1 practice problem ↓

Task groups, setup and teardown

A DAG with forty tasks is hard to read and maintain. Task groups fold related tasks into one box, and setup and teardown tasks make sure temporary resources are always cleaned up. This lesson shows both.

Pipelines in production · Airflow · Silver · Scaling a DAG, lesson 2 of 2

Read first: Trigger rules and run state (skip if you know it)

In plain words first

A long to-do list becomes readable once you group it under headings: "Kitchen", "Garden", "Paperwork". And some jobs come in pairs: if you switch the oven on, someone must switch it off, whatever happens in between.

Tasktask: One step of a DAG, such as running a query or calling an API. Each run of a task for one date is a task instance. Learn more → groups are the headings; setup and teardown tasks are the oven on and off.

Groups for structure

A DAGDAG: A pipeline in Airflow: a set of tasks and the order they run in, defined in a Python file and run on a schedule. Learn more → with forty tasks is hard to read. A task group collapses related tasks into one box in the UI, prefixes their ids with the group's name, and lets you set dependencies on the group as a whole: extract >> transform_group connects extract to the first tasks inside the group, and transform_group >> notify connects the group's last tasks to notify.

Python · dags/label_reports.py · one group per report, run in parallel
import pendulum
from airflow.sdk import DAG
from airflow.providers.standard.operators.bash import BashOperator
from airflow.sdk import TaskGroup

with DAG(
    dag_id="label_reports",
    schedule="@daily",
    start_date=pendulum.datetime(2026, 3, 1, tz="UTC"),
):
    snapshot = BashOperator(
        task_id="snapshot_plays", bash_command="snapshot_plays --day {{ ds }}"
    )

    with TaskGroup("streams") as streams:
        s_build = BashOperator(
            task_id="build", bash_command="report streams --day {{ ds }}"
        )
        s_send = BashOperator(
            task_id="send", bash_command="send_report streams --day {{ ds }}"
        )
        s_build >> s_send

    with TaskGroup("royalties") as royalties:
        r_build = BashOperator(
            task_id="build", bash_command="report royalties --day {{ ds }}"
        )
        r_send = BashOperator(
            task_id="send", bash_command="send_report royalties --day {{ ds }}"
        )
        r_build >> r_send

    snapshot >> [streams, royalties]

What the schedulerscheduler: The Airflow process that reads DAG files, creates runs when they are due, and queues the tasks whose upstream tasks are done. Learn more → does · deployed Mar 2, 08:00 UTC · grid as of Mar 2, 09:00 UTC · failing: royalties.build · @daily (CronTriggerTimetable)

task \ runMar 2
00:00
snapshot_plays
streams.build
royalties.build
streams.send
royalties.send

successfailedupstream failed· the bar on top is the run's state; a number is how many tries

Both groups have a build and a send; the prefixes (streams.build, royalties.build) keep the ids unique. When royalties.build fails, only its own send is affected. Groups are only structure: they add no scheduling behaviour. A function decorated with @task_group builds a group each time it is called, which is handy for repeating the same shape.

Setup and teardown

Some tasks create a resource that must always be removed: a temporary cluster, a staging schema, a scratch table. Marking them as setup and teardown makes that explicit:

Python · a cluster that is always deleted
create = BashOperator(
    task_id="create_cluster", bash_command="cluster up --name charts-{{ ds_nodash }}"
)
job = BashOperator(
    task_id="run_job", bash_command="spark-submit charts.py --day {{ ds }}"
)
delete = BashOperator(
    task_id="delete_cluster", bash_command="cluster down --name charts-{{ ds_nodash }}"
)

create >> job >> delete.as_teardown(setups=create)
  • The teardown runs once the work between setup and teardown has finished, whatever happened to it, like all_done, as long as its setup succeeded.
  • A teardown is ignored when the run's state is decided: if run_job fails, the run fails, even though the teardown succeeded. This is what a plain all_done clean-up task gets wrong (Trigger rulestrigger rule: The condition that lets a task start, based on its upstream tasks: all succeeded (the default), one failed, all done, and so on. Learn more → and run state).
  • Clearing a task inside the scope for a rerun also clears its setup and teardown, so the resource is created and removed again around the rerun.
  • In the UI, setup and teardown tasks are drawn with arrows and dotted lines that show what they wrap.
Note: SubDAGs, the old way to nest DAGs, were removed in Airflow 3. Task groups replace them for structure; for reuse across DAGs, share Python functions that build task groups.

Common mistakes

  • A clean-up task with all_done as a leaf — The run succeeds when the work failed. Use a teardown.
  • Expecting a task group to retry or run as a unit — Groups are only structure; retries and states are per task.
  • Repeating the same tasks by copy and paste — A @task_group function builds the same shape each time it is called.
  • Waiting for SubDAGs — Removed in Airflow 3.

Key takeaways

  • Task groups organise a DAG, prefix task ids and accept dependencies as a whole.
  • Groups add no scheduling behaviour.
  • Setup and teardown pair a resource with its clean-up.
  • Teardowns run like all_done but do not decide the run's state.

Interview prep

THE QUESTION

"How do you make sure a temporary cluster is always deleted, without the run looking successful when the job failed?"

Make create_cluster a setup and delete_cluster its teardown: create >> job >> delete.as_teardown(setups=create). The teardown runs after the work whatever happened to it, like all_done, but teardowns are ignored when Airflow computes the run state, so a failed job still fails the run. Clearing the job for a rerun also clears the setup and teardown, so the cluster is recreated and removed around the rerun. The older alternative is an all_done clean-up plus a watcher task that fails the run.

Avoid: "put the clean-up in a task group so it always runs". Groups are structure only; they change nothing about when tasks run.

Follow-ups · answer out loud, then open

Follow-up"Can two task groups contain tasks with the same task_id?"
Yes: the group's id prefixes its tasks' ids (streams.build, royalties.build), so they stay unique in the DAG.
Scenario"You rerun only the Spark job of yesterday's run, which needs the temporary cluster. What happens with setup and teardown?"
Clearing a task inside a setup/teardown scope also clears its setup and teardown, so the rerun recreates the cluster, runs the job, and deletes the cluster again.
Trap"A failed teardown always fails the run."
By default teardowns are ignored when the run state is decided, so a failed clean-up does not fail the run; set on_failure_fail_dagrun=True on the teardown if it should.

Check yourself

3 questions

A task "build" inside the task group "royalties" has which task_id?

Show the answer

royalties.build. Group names prefix their tasks' ids.

What does a >> group connect a to?

Show the answer

The group's first tasks (those with no upstream inside it). Dependencies on a group attach to its roots or leaves.

The work fails and the teardown succeeds. What is the run's state?

Show the answer

failed. Teardowns are ignored when the run state is decided.

Practise this

Problems that use this, graded on hidden tests

Keep going

Up next · new module: Data-aware pipelines · 5 min read
Assets: scheduling on data
Start a DAG when the data it needs has been updated, instead of at a guessed time.

Related lessons

Previous: Dynamic task mapping

Primary sources: Task groups · Setup and teardown