I am creating DAGs dynamically, following instructions found in Dynamically Generating DAGs in Airflow, modifying the number of dags to be created via a variable k:
from datetime import datetime
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
def create_dag(dag_id,
schedule,
dag_number,
default_args):
def hello_world_py(*args):
print('Hello World')
print('This is DAG: {}'.format(str(dag_number)))
dag = DAG(dag_id,
schedule_interval=schedule,
default_args=default_args)
with dag:
t1 = PythonOperator(
task_id='hello_world',
python_callable=hello_world_py,
dag_number=dag_number)
return dag
# build k dags
k = 5
for n in range(1, k + 1):
dag_id = 'hello_world_{}'.format(str(n))
default_args = {'owner': 'airflow',
'start_date': datetime(2018, 1, 1)
}
schedule = '@daily'
dag_number = n
globals()[dag_id] = create_dag(dag_id,
schedule,
dag_number,
default_args)
I can check the created DAGs with the UI and the CLI. Both are in sync:
> airflow dags list
dag_id | filepath | owner | paused
==============+================+=========+=======
hello_world_1 | hello_world.py | airflow | True
hello_world_2 | hello_world.py | airflow | True
hello_world_3 | hello_world.py | airflow | True
hello_world_4 | hello_world.py | airflow | True
hello_world_5 | hello_world.py | airflow | True
Now, if I decrease k to 3, the CLI lists only 3 dags as expected. However the UI keeps showing 5 dags.
How to keep the UI in sync with the number of dags to be created? How to delete DAGs programmatically in python? I would like to delete DAGs as easily as I create them.