vatsrahul1001 opened a new issue, #52147: URL: https://github.com/apache/airflow/issues/52147
### Apache Airflow version 3.0.2 ### If "Other Airflow 2 version" selected, which one? _No response_ ### What happened? When I tried running `airflow db clean -t dag --clean-before-timestamp 2025-06-25` I saw the following error ``` The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/usr/local/bin/airflow", line 10, in <module> sys.exit(main()) ^^^^^^ File "/opt/airflow/airflow-core/src/airflow/__main__.py", line 55, in main args.func(args) File "/opt/airflow/airflow-core/src/airflow/cli/cli_config.py", line 48, in command return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/opt/airflow/airflow-core/src/airflow/utils/cli.py", line 112, in wrapper return f(*args, **kwargs) ^^^^^^^^^^^^^^^^^^ File "/opt/airflow/airflow-core/src/airflow/utils/providers_configuration_loader.py", line 55, in wrapped_function return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/opt/airflow/airflow-core/src/airflow/cli/commands/db_command.py", line 279, in cleanup_tables run_cleanup( File "/opt/airflow/airflow-core/src/airflow/utils/session.py", line 101, in wrapper return func(*args, session=session, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/airflow/airflow-core/src/airflow/utils/db_cleanup.py", line 558, in run_cleanup _cleanup_table( File "/opt/airflow/airflow-core/src/airflow/utils/db_cleanup.py", line 407, in _cleanup_table _cleanup_single_table( File "/opt/airflow/airflow-core/src/airflow/utils/db_cleanup.py", line 386, in _cleanup_single_table _do_delete( File "/opt/airflow/airflow-core/src/airflow/utils/db_cleanup.py", line 274, in _do_delete raise e File "/opt/airflow/airflow-core/src/airflow/utils/db_cleanup.py", line 270, in _do_delete session.execute(delete) File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 1717, in execute result = conn._execute_20(statement, params or {}, execution_options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1710, in _execute_20 return meth(self, args_10style, kwargs_10style, execution_options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/sql/elements.py", line 334, in _execute_on_connection return connection._execute_clauseelement( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1577, in _execute_clauseelement ret = self._execute_context( ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1953, in _execute_context self._handle_dbapi_exception( File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 2134, in _handle_dbapi_exception util.raise_( File "/usr/local/lib/python3.12/site-packages/sqlalchemy/util/compat.py", line 211, in raise_ raise exception File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1910, in _execute_context self.dialect.do_execute( File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 736, in do_execute cursor.execute(statement, parameters) sqlalchemy.exc.IntegrityError: (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)=(01979cd6-24b4-739e-83a7-ca5876085ee8) is still referenced from table "task_instance". [SQL: DELETE FROM dag USING _airflow_deleted__dag__20250623125028 WHERE dag.dag_id = _airflow_deleted__dag__20250623125028.dag_id] (Background on this error at: https://sqlalche.me/e/14/gkpj) root@15fa45f1b905:/opt/airflow# ``` ### What you think should happen instead? _No response_ ### How to reproduce Execute below command `airflow db clean -t dag --clean-before-timestamp 2025-06-25` ### Operating System os ### Versions of Apache Airflow Providers _No response_ ### Deployment Astronomer ### 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 - [x] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org