Repository navigation
Closed
Labels
affected_version:3.0Issues Reported for 3.0affected_version:3.1Issues Reported for 3.1area:MetaDBMeta Database related issues.area:corekind:bugThis is a clearly a bug
Description
Apache Airflow version
3.1.0
If "Other Airflow 2 version" selected, which one?
No response
What happened?
The command is used to clean up the records created for 30 days+ :
airflow db clean --clean-before-timestamp "$(date -d "30 days ago" +%Y-%m-%dT%H:%M)" --skip-archive --yes
The error :
Checking table dag_version
Found 155 rows meeting deletion criteria.
Performing Delete...
Moving data to table _airflow_deleted__dag_version__20250929024208
self.dialect.do_execute(
File "/home/airflow/.local/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 736, in do_execute
cursor.execute(statement, parameters)
psycopg2.errors.ForeignKeyViolation: update or delete on table "dag_version" violates foreign key constraint "task_instance_dag_version_id_fkey" on table "task_instance"
DETAIL: Key (id)=(0198edf9-f9f1-7d73-92c1-fac5359c10b7) is still referenced from table "task_instance".
For the DAG Version ID 0198edf9-f9f1-7d73-92c1-fac5359c10b7, I found :-
- 1 record in
dag_versionwithlast_updated= 2025-08-28 (more than 1 month ago, should be scoped) - some records in
task_instancewithupdated_at= 2025-09-02 (less than 1 month ago, should be not scoped)
Seem I can workaround by retrying it after 2025-10-02 so all mentioned records will be scoped to remove.
What you think should happen instead?
It should skip the problematic records / tables, or aggressively remove the referencing records as well.
How to reproduce
n/a
Operating System
AlmaLinux release 9.6 (Sage Margay)
Versions of Apache Airflow Providers
No response
Deployment
Official Apache Airflow Helm Chart
Deployment details
No response
Anything else?
No response
Are you willing to submit PR?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project's Code of Conduct
Reacted by 鐘翊修, Krzysztof Taborski, Tomas Sedlak, A N, Roberto Gelsi, vcoronas, Manu López, fbmatilla, Tomer Khalili, networkgangster and 7 more
Reacted by lamiskin
Metadata
Metadata
Assignees
Labels
affected_version:3.0Issues Reported for 3.0affected_version:3.1Issues Reported for 3.1area:MetaDBMeta Database related issues.area:corekind:bugThis is a clearly a bug
Activity
boring-cyborg commentedon Sep 29, 2025
Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.
HsiuChuanHsu commentedon Oct 11, 2025
Hi @hket-ianchu,
I was able to reproduce the issue you provided. Below are the reproduction results:
Create testing data in
dag_version&task_instancetable.dag_version: created_at | 2025-09-01task_instance-1: start_date | 2025-09-06task_instance-2: start_date | 2025-10-01Use
airflow db cleanto clean up the recordsairflow db clean --clean-before-timestamp "2025-09-21T10:30" --skip-archive --yesResult
HsiuChuanHsu commentedon Oct 11, 2025
I will sumbit a PR to refine Cleanup Logic.
Refined Cleanup Logic:
dag_version, we must check for any remainingtask_instancereferences.dag_versionwill be skipped (similar to the suggested "skip problematic records" approach).r-richmond commentedon Oct 21, 2025
@HsiuChuanHsu Thank you for submitting a PR for this. Just wanted to say that I've experienced this in production as well.
Stack Trace
airlfow db clean --clean-before-timestamp --skip-archive TIMESTAMPHsiuChuanHsu commentedon Oct 21, 2025
@r-richmond Yes, exact same error message! Thanks for letting me know.
hket-ianchu commentedon Oct 27, 2025
@HsiuChuanHsu Thanks for your help~!
j-cull commentedon Dec 12, 2025
Is this going to be added into 3.1.5?
9 remaining items
kang-seongbeom commentedon May 7, 2026
I'm not sure if this is the right way to use it, but dropping the foreign key constraint on dag_version in the task_instance table did resolve the above issue.....
sw-cyderes commentedon May 13, 2026
Is this safe to do?
kang-seongbeom commentedon May 13, 2026
This is unsafe and can cause problems during version upgrades. Although there is a workaround, it requires forcibly modifying existing database data.
TKul6 commentedon May 17, 2026
@sw-cyderes I did a safer (and more dummy) patch (until the issue will be fixed).
the cleaner can get an array of tables names to purge, I just provided all the table except the
dag_version.so the table won't be cleaned, but at least in my case it does not contain a lot of data, so as a patch it is good enough for me.
kang-seongbeom commentedon May 17, 2026
In the #63703 issue, there is a command to perform a cleanup excluding the dag_version and dag tables.
j-cull commentedon Jun 5, 2026
What am I missing here? Why is this not seen as an important fix? An airflow instance will have degraded performance over time if airflow db clean is not ran regularly.
alkismavridis commentedon Jun 8, 2026
We are also affected by this. Are there any news? Seems like a non-trivial bug.
potiuk commentedon Jun 14, 2026
Fix is coming by #68339
igorlovich commentedon Jul 17, 2026
Still failing with 3.3.0
exec airflow db clean --clean-before-timestamp "${CLEAN_TS}" --yes --skip-archive --verbosesqlalchemy.exc.IntegrityError: (psycopg2.errors.RestrictViolation) update or delete on table "dag_version" violates RESTRICT setting of foreign key constraint "task_instance_dag_version_id_fkey" on table "task_instance" DETAIL: Key (id)=(019a2639-6675-74ec-bafd-a4f6b7a58a07) is referenced from table "task_instance".wufh43 commentedon Aug 4, 2026
@ephraimbuddy We also experience that the issue remains in 3.3.0
ephraimbuddy commentedon Aug 4, 2026
@igorlovich @wufh43 , this particular issue is about cleaning the dag version table. If you had used --tables and specifically selected dag version it will work. Now the one you experience now seems to come from the dag. I will look into it or you can create a new issue with full log