How to Delete a DAG in Apache Airflow Quickly and Safely
To delete a DAG in Airflow, first remove the DAG file from the DAGs folder. Then, clear the DAG's metadata from the Airflow database using the
airflow dags delete command or manually via the Airflow UI to fully remove it.Syntax
Deleting a DAG involves two main steps: removing the DAG file and clearing its metadata. The key command to clear metadata is:
airflow dags delete <dag_id> --yesHere, <dag_id> is the identifier of the DAG you want to delete. The --yes flag skips confirmation prompts.
bash
airflow dags delete <dag_id> --yes
Example
This example shows how to delete a DAG named example_dag. First, delete the DAG file from your DAGs folder. Then run the command below to remove its metadata from Airflow's database.
bash
airflow dags delete example_dag --yes
Output
Are you sure you want to delete DAG example_dag? [y/N]: y
Deleted DAG: example_dag
Common Pitfalls
- Not removing the DAG file: Airflow will keep detecting the DAG if the file remains in the DAGs folder.
- Not clearing metadata: The DAG will still appear in the Airflow UI and database if metadata is not deleted.
- Forgetting to restart scheduler/webserver: Sometimes a restart is needed to refresh the UI after deletion.
bash
## Wrong: Only deleting metadata but not the DAG file airflow dags delete example_dag --yes ## Right: Delete DAG file first, then metadata rm ~/airflow/dags/example_dag.py airflow dags delete example_dag --yes
Quick Reference
Summary tips for deleting a DAG:
- Remove the DAG file from the DAGs folder.
- Run
airflow dags delete <dag_id> --yesto clear metadata. - Restart Airflow scheduler and webserver if needed.
- Verify deletion by checking the Airflow UI and database.
Key Takeaways
Always remove the DAG file from the DAGs folder to stop Airflow from detecting it.
Use the command 'airflow dags delete --yes' to clear DAG metadata from the database.
Restart Airflow services to refresh the UI and scheduler after deletion.
Verify the DAG is fully deleted by checking the Airflow UI and database.
Deleting only the file or only metadata is not enough; both steps are required.