0

The Purpose is to delete airflow dag from UI with rest api call. I tried the api call .../delete?dag_id=<dag_id> ,this rest call fails saying CSRF token is missing. How to get CSRF token from airflow server ? or is there a better way to delete dag from airflow UI in automated way ? (not manually pressing delete button from airflow UI)

user4157124
  • 2,809
  • 13
  • 27
  • 42

1 Answers1

0

for airflow vesrion >= 1.10 you can use the experimental REST API

here is a code example:

import requests
from urllib.parse import urljoin

def delete_dag(airflow_server_address ,dag_id):
    url = urljoin(airflow_server_address, '/api/experimental/dags/'+dag_id)
    requests.delete(url)

this will delete the dag runs and data from the database. make sure you delete the dag .py file first, otherwise the dag will continue to appear in the airflow GUI.

  • this works, but i had to use airflow delete_dag cli command, since anyway i have to run cli command to delete .py file – Hemanth Kumar Sep 07 '20 at 19:04
  • you can use the cli commend as well. but you asked for deleting using rest api call. @HemanthKumar – Oded Rozencweig Sep 08 '20 at 08:10
  • Using the rest api works only if the underlying .py file is deleted else the UI will still have the entry, thats what i meant. Thank you – Hemanth Kumar Sep 13 '20 at 11:38
  • you right, it's written in the answer. please mark as accepted answer if you find it correct. – Oded Rozencweig Sep 13 '20 at 11:48
  • stack overflow says i don't have enough reputation do that action. this answer is accepted from my side. Thanks. – Hemanth Kumar Sep 14 '20 at 05:37
  • Hi @Oded Rozencweig , i'm getting a 404 error for the Delete Rest API that you have suggested. Im using 1.10.9 version. Im able to run other Rest API but not this one. Am i missing something ? – Hemanth Kumar Mar 08 '21 at 06:51