Airflow Logs Cleanup

Python utility or Airflow DAG that deletes old Airflow log files and empty folders.

Trim old Airflow log files automatically, before disk pressure becomes an incident

Airflow Logs Cleanup is a small Python utility you can run as a standalone script or schedule as an Airflow DAG. It targets the usual buildup of rotated logs (including numbered dag_processor_manager rotations) and general log files past a retention window, then tidies empty folders left behind, so operators spend less time on manual housekeeping.

When it is useful

Your Airflow home is accumulating log volume, you want a repeatable cleanup policy instead of one-off rm sessions, or you prefer a DAG-driven job (the repository ships an example schedule you can change). You need AIRFLOW_HOME set and a supported Python runtime as described in the project.

What you can do

  • Delete rotated dag_processor_manager log files (the .1, .2, … pattern the README calls out).
  • Remove log files older than a configurable age (the default in-repo window is documented there; you can extend it by editing the retention constant).
  • Prune empty directories after deletions, with safeguards called out for sensitive paths.
  • Run ad hoc with the cleanup script or install the DAG into your Airflow dags folder and enable it when you are ready.

Limits

  • This does not replace your broader logging, monitoring, or compliance retention policy; it automates file cleanup based on the rules in the repository.
  • Savings and performance depend on your cluster size, log volume, and schedule; this page does not promise specific percentages or dollar amounts.
  • Permissions, Airflow version quirks, and backup requirements are still your responsibility before enabling in production.

Frequently asked questions

What does Airflow Logs Cleanup delete?

It deletes rotated dag_processor_manager logs (for example .log.1, .log.2), removes general log files older than the retention window (default 7 days via MILLISECONDS_TO_KEEP), and prunes empty directories afterward (excluding dag_processor_manager).

Can I run it as a script or a DAG?

Both. Run python cleanup_logs.py ad hoc, or copy cleanup_logs_dag.py into $AIRFLOW_HOME/dags/, enable cleanup_logs_dag in the UI. The example schedule is daily at 3:00 AM (0 3 * * *).

What environment does it need?

Python 3.8+, Apache Airflow with PythonOperator support, and AIRFLOW_HOME set. Missing AIRFLOW_HOME and common filesystem errors are logged without stopping the whole cleanup.

How do I change retention?

Edit MILLISECONDS_TO_KEEP in cleanup_logs.py or cleanup_logs_dag.py (for example 14 * 86400 * 1000 for 14 days). This automates file cleanup only; broader retention and backups remain your policy.

Continue exploring