iosif2 opened a new issue, #53551:
URL: https://github.com/apache/airflow/issues/53551

   ### Apache Airflow version
   
   3.0.3
   
   ### If "Other Airflow 2 version" selected, which one?
   
   _No response_
   
   ### What happened?
   
   ## This issue addresses error encountered when deleting dags via both the 
CLI and the web UI.
   
   ### 1. ForeignKeyViolation on backfill table deletion
    - However executing the deletion twice as separate operations resolves the 
issue and allows for successful deletion.
    - Root Cause: This error occurs randomly due to how the delete_dag function 
operates. I suspect that get_sqla_model_classes does not consistently return 
models in the correct dependency order.
   #### Command Line Output for this error:
   ```
   root@f6650f6a4c58:/opt/airflow# airflow dags delete test_dag2
   This will drop all existing records related to the specified DAG. Proceed? 
(y/n)y
   [2025-07-18T13:21:57.427+0000] {delete_dag.py:53} INFO - Deleting DAG: 
test_dag2
   Traceback (most recent call last):
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1910, in _execute_context
       self.dialect.do_execute(
     File 
"/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 
736, in do_execute
       cursor.execute(statement, parameters)
   psycopg2.errors.ForeignKeyViolation: update or delete on table "backfill" 
violates foreign key constraint "dag_run_backfill_id_fkey" on table "dag_run"
   DETAIL:  Key (id)=(2) is still referenced from table "dag_run".
   
   
   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 113, in 
wrapper
       return f(*args, **kwargs)
     File 
"/opt/airflow/airflow-core/src/airflow/utils/providers_configuration_loader.py",
 line 54, in wrapped_function
       return func(*args, **kwargs)
     File "/opt/airflow/airflow-core/src/airflow/cli/commands/dag_command.py", 
line 103, in dag_delete
       message = api_client.delete_dag(dag_id=args.dag_id)
     File "/opt/airflow/airflow-core/src/airflow/api/client/local_client.py", 
line 74, in delete_dag
       count = delete_dag.delete_dag(dag_id)
     File "/opt/airflow/airflow-core/src/airflow/utils/session.py", line 100, 
in wrapper
       return func(*args, session=session, **kwargs)
     File "/opt/airflow/airflow-core/src/airflow/api/common/delete_dag.py", 
line 70, in delete_dag
       count += session.execute(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", 
line 1717, in execute
       result = conn._execute_20(statement, params or {}, execution_options)
     File "/usr/local/lib/python3.10/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.10/site-packages/sqlalchemy/sql/elements.py", 
line 334, in _execute_on_connection
       return connection._execute_clauseelement(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1577, in _execute_clauseelement
       ret = self._execute_context(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1953, in _execute_context
       self._handle_dbapi_exception(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 2134, in _handle_dbapi_exception
       util.raise_(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", 
line 211, in raise_
       raise exception
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1910, in _execute_context
       self.dialect.do_execute(
     File 
"/usr/local/lib/python3.10/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 "backfill" violates foreign key constraint 
"dag_run_backfill_id_fkey" on table "dag_run"
   DETAIL:  Key (id)=(2) is still referenced from table "dag_run".
   
   [SQL: DELETE FROM backfill WHERE backfill.dag_id = %(dag_id_1)s RETURNING 
backfill.id]
   [parameters: {'dag_id_1': 'test_dag2'}]
   (Background on this error at: https://sqlalche.me/e/14/gkpj)
   ```
   
   ### 2. ForeignKeyViolation on other tables (related to dag_version)
    - Root Cause: It suggests that deleting dag_version records is blocked 
because they are still referenced by the task_instance table.
    - Proposed Solution: I believe the RESTRICT ON DELETE behavior is 
unnecessary for this table; thus, I have modified it to CASCADE to resolve this 
issue.
   
   #### Command Line Output for this error:
   ```
   root@f6650f6a4c58:/opt/airflow/airflow-core/src/airflow# airflow dags delete 
test_dag1
   This will drop all existing records related to the specified DAG. Proceed? 
(y/n)y
   [2025-07-18T13:39:21.439+0000] {delete_dag.py:53} INFO - Deleting DAG: 
test_dag1
   Traceback (most recent call last):
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1910, in _execute_context
       self.dialect.do_execute(
     File 
"/usr/local/lib/python3.10/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)=(01981dc1-c244-7368-bd32-44242bc82280) is still referenced 
from table "task_instance".
   
   
   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 113, in 
wrapper
       return f(*args, **kwargs)
     File 
"/opt/airflow/airflow-core/src/airflow/utils/providers_configuration_loader.py",
 line 54, in wrapped_function
       return func(*args, **kwargs)
     File "/opt/airflow/airflow-core/src/airflow/cli/commands/dag_command.py", 
line 103, in dag_delete
       message = api_client.delete_dag(dag_id=args.dag_id)
     File "/opt/airflow/airflow-core/src/airflow/api/client/local_client.py", 
line 74, in delete_dag
       count = delete_dag.delete_dag(dag_id)
     File "/opt/airflow/airflow-core/src/airflow/utils/session.py", line 100, 
in wrapper
       return func(*args, session=session, **kwargs)
     File "/opt/airflow/airflow-core/src/airflow/api/common/delete_dag.py", 
line 70, in delete_dag
       count += session.execute(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", 
line 1717, in execute
       result = conn._execute_20(statement, params or {}, execution_options)
     File "/usr/local/lib/python3.10/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.10/site-packages/sqlalchemy/sql/elements.py", 
line 334, in _execute_on_connection
       return connection._execute_clauseelement(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1577, in _execute_clauseelement
       ret = self._execute_context(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1953, in _execute_context
       self._handle_dbapi_exception(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 2134, in _handle_dbapi_exception
       util.raise_(
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", 
line 211, in raise_
       raise exception
     File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", 
line 1910, in _execute_context
       self.dialect.do_execute(
     File 
"/usr/local/lib/python3.10/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)=(01981dc1-c244-7368-bd32-44242bc82280) is still referenced 
from table "task_instance".
   
   [SQL: DELETE FROM dag_version WHERE dag_version.dag_id = %(dag_id_1)s 
RETURNING dag_version.id]
   [parameters: {'dag_id_1': 'test_dag1'}]
   (Background on this error at: https://sqlalche.me/e/14/gkpj)
   ```
   
   
   ### What you think should happen instead?
   
   _No response_
   
   ### How to reproduce
   
   ### 1. ForeignKeyViolation on backfill table deletion
    - Reproduction Steps: first backfill any DAG. Then, execute airflow dags 
delete [dag_id]. This error occurs randomly due to how the delete_dag function 
operates.
   ### 2. ForeignKeyViolation on other tables (related to dag_version)
    - Reproduction Steps: While I am not entirely certain how to consistently 
reproduce this specific error, it appears alongside the backfill deletion error 
described above.
   
   ### Operating System
   
   macOS 15.5 (24F74)
   
   ### Versions of Apache Airflow Providers
   
   _No response_
   
   ### Deployment
   
   Docker-Compose
   
   ### Deployment details
   
   docker compose with both latest released image and main branch
   
   ### Anything else?
   
   I have already opened PR #53501  related to this issue. That PR includes 
changes to the ON DELETE behavior for the `dag_run` and `task_instance` tables. 
However, modifying the delete_dag function to manually sort the delete target 
models would be a better solution than my current approach.
   
   ### Are you willing to submit PR?
   
   - [x] 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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to