In addition to storing configurations about your Airflow environment, the Airflow metadata database stores data about past and present task runs. Airflow never automatically removes metadata, so the longer you use it, the more task run data is stored in your metadata DB. Over a long enough time, this can result in a bloated metadata DB, which can affect performance across your Airflow environment.
When a table in the metadata DB is larger than 50GiB, you might start to experience degraded scheduler performance. This can result in:
The following tables in the database are at risk of becoming too large over time:
dag_runjoblogrendered_task_instance_fieldstask_instancexcomTo keep your Airflow environment running at optimal performance, you can clean the metadata DB using the Airflow CLI airflow db clean command. This command was created as a way to safely clean up your metadata DB without querying it directly. This tutorial describes how to implement a cleanup DAG in Airflow so that you can clean your database using the command directly from the Airflow UI.
Even when using airflow db clean, deleting data from the metadata database can destroy important data. Read the Warnings section carefully before implementing this tutorial DAG in any production Airflow environment.
Deleting data from the metadata database can be an extremely destructive action. If you delete data that future task runs depend on, it’s difficult to restore the database to its previous state without interrupting your data pipelines. Before implementing the DAG in this tutorial, consider the following:
clean_before_timestamp value, use as old a date as possible. The older the deleted data, the less likely it is to affect your currently running DAGs.--skip-archive option and does not maintain any history. If the task fails for example if it runs for longer than five minutes, the archive tables are not cleared. By running airflow db drop-archived in the second task of the DAG, we ensure all archive tables are dropped even in the event of the first task failing.This DAG has been designed and optimized for Airflow environments running on Astro. Consider adjusting the parameters and code if you’re running the DAG in any other type of Airflow environment.
In your dags folder, create a file called db_cleanup.py.
Copy the following code into the file.
Rather than running on a schedule, this DAG is triggered manually by default and includes params so that you’re in full control over how you clean the metadata DB.
It includes two tasks:
clean_db, that runs airflow db cleanclean_archive_tables, that runs airflow db drop-archivedThese two tasks run with params you specify at runtime. The params let you specify:
In this step, run the DAG in a local Airflow environment to practice the workflow for cleaning metadata DB data. When completing this process in a production environment, you would complete this process only after your Airflow environment has been running for a while.
Run astro dev start in your Astro project to start Airflow, then open the Airflow UI at localhost:8080.
In the Airflow UI, run the db_cleanup DAG by clicking the play button, then click Trigger DAG w/ Config. Configure the following params:
dry_run: truetables: all_tablesclean_before_timestamp: (datetime.now(tz=UTC) - timedelta(days=90)).isoformat()Click Trigger.
After the task completes, click on the Graph tab.
Click on the clean_db task.
Click on the Logs tab.
Check that the airflow db cleanup command completed successfully. Note that if you created a new Astro project for this tutorial, the run will not show much data to be deleted.
You can now use this DAG to periodically clean data from the Airflow metadata DB as needed.