Skip to content

Prevent db clean from deleting Dags with surviving task instances - #74659

Open
ucaeon wants to merge 1 commit into
apache:mainfrom
ucaeon:fix-71864-db-clean
Open

ucaeon wants to merge 1 commit into
apache:mainfrom
ucaeon:fix-71864-db-clean

Conversation

@ucaeon

@ucaeon ucaeon commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

airflow db clean can fail when cleaning up a stale Dag with task instances that have never started.

For example, manually triggering a paused Dag can leave a queued Dag run and task instances with NULL start dates. If the Dag file is subsequently removed and the Dag becomes eligible for cleanup, the following can happen:

  1. The task instances survive cleanup because their start_date is NULL.
  2. The Dag is selected for deletion based on its last_parsed_time.
  3. Deleting the Dag cascades to its dag_version rows, including versions still referenced by the surviving task instances.
  4. The database rejects the deletion with a foreign-key constraint violation.

Protecting referenced versions during dag_version cleanup alone is insufficient because deleting the parent Dag can still cascade to those versions.

This change reuses the existing skip_if_referenced mechanism with task_instance.dag_id to exclude Dags that still have task instances. The reference check applies both when selecting cleanup candidates and immediately before deletion. Dags without surviving task instances remain eligible for cleanup.

Regression tests verify that:

  • Referenced Dags, pinned versions, and never-started task instances are preserved, while unreferenced Dags and their versions are deleted.
  • Cleanup works with and without archiving, and with and without an explicit batch size.
  • A task instance added between candidate selection and deletion prevents its Dag from being deleted.

All five new test cases fail with a foreign-key constraint violation without the fix.

This PR addresses the cleanup failure in #71864. Deleting records with NULL start dates is a separate change tracked in #69872 and discussed in #69879.

closes: #71864

Validation

  • test_db_cleanup.py: PostgreSQL 120 passed, MySQL 119 passed, SQLite 118 passed, with backend-specific skips.
  • Selective DB suites: API, Other, Core, CLI, Serialization, and Always passed on SQLite.
  • Selective non-DB suite: 4,988 passed.
  • Regular and manual static checks passed.
  • apache-airflow documentation build passed.

Was generative AI tooling used to co-author this PR?
  • Yes — Codex (GPT-6)

Generated-by: Codex (GPT-6) following the guidelines

Preserve Dags while task instances still reference them so cascading version deletion cannot violate task instance foreign keys. Reuse the existing reference guard during candidate selection and deletion.
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the stale Stale PRs per the .github/workflows/stale.yml policy file label Nov 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale Stale PRs per the .github/workflows/stale.yml policy file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ForeignKeyViolation when doing airflow db clean (dag table)

1 participant