Fix airflow db clean silently skipping three tables when scoped to Dags by kaxil · Pull Request #73173 · apache/airflow · GitHub
Skip to content

Fix airflow db clean silently skipping three tables when scoped to Dags - #73173

Open
kaxil wants to merge 1 commit into
apache:mainfrom
astronomer:db-clean-deadline-dag-scope
Open

kaxil wants to merge 1 commit into
apache:mainfrom
astronomer:db-clean-deadline-dag-scope

Conversation

@kaxil

@kaxil kaxil commented Sep 15, 2026

Copy link
Copy Markdown
Member

Follow-up to #70923, which restored the coverage check for db clean. That check compares table names against the config, not the columns the config names, so it cannot catch this.

airflow db clean --dag-ids or --exclude-dag-ids silently purges nothing from three tables. Each names a dag_id column it does not have: task_reschedule.dag_id was dropped in 3.0.0, deadline.dag_id in 3.1.0, and asset_event has only ever had source_dag_id. The query fails on a missing column, run_cleanup suppresses a failing table into a warning, and the command exits 0. An operator who always scopes by Dag has been growing those three tables forever while cleanup reported success.

deadline and task_reschedule now reach their Dag the way the schema does, through dag_run and task_instance.

Why scope them rather than just drop the filter, which would be the smaller diff: deadline is cleaned as a dependent of dag_run precisely so its rows are archived before the ON DELETE CASCADE removes them. An unscoped pass would purge deadlines belonging to the very Dags whose runs --exclude-dag-ids is preserving, turning a silent no-op into silent data loss.

--exclude-dag-ids now also treats a NULL dag id as eligible. A row attributed to no Dag is not one of the excluded Dags' rows, but NOT IN alone yields NULL for it and retains it forever. That is every job row, since core never sets Job.dag_id, and it becomes reachable for asset_event and log too. This changes behaviour for those tables, in the direction the flag already documents.

The guard test is the part that stops this recurring. It only ever called run_cleanup without Dag filters, and the Dag filter is the only thing that dereferences these column names, so all three configs drifted unnoticed across two releases. It is now parametrized over the filter combinations and fails on each of them.

The cleanup config named a dag_id column on three tables that do not have one.
task_reschedule.dag_id was dropped in 3.0.0, deadline.dag_id in 3.1.0, and
asset_event has only ever had source_dag_id. Any run passing --dag-ids or
--exclude-dag-ids built SQL against a column that does not exist, and run_cleanup
suppresses a failing table into a warning, so the command reported success and
exited 0 while never purging a row from any of them.

deadline and task_reschedule now reach their Dag the way the schema does, through
dag_run and task_instance, so a table with no dag id of its own can be scoped
through a foreign key. Dropping the filter instead would have been worse for
deadline: it is cleaned as a dependent of dag_run precisely so its rows are
archived before the ON DELETE CASCADE removes them, so an unscoped pass would
purge deadlines belonging to the very Dags whose runs --exclude-dag-ids preserves.

--exclude-dag-ids now also treats a NULL dag id as eligible. A row attributed to no
Dag is not one of the excluded Dags' rows, but NOT IN alone yields NULL for it and
retains it forever. That was every job row, since core never sets Job.dag_id, and it
becomes reachable for asset_event and log too.

The guard test only ever called run_cleanup without Dag filters, which is the branch
that dereferences these column names, so all three configs drifted unnoticed across
two releases. It is now parametrized over the filter combinations and fails on each
of them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant