Skip to content

Instantly share code, notes, and snippets.

@DSdatsme
Created October 6, 2022 11:09
Show Gist options
  • Save DSdatsme/37c0b8b86343004399b3dcbef35c9983 to your computer and use it in GitHub Desktop.
Save DSdatsme/37c0b8b86343004399b3dcbef35c9983 to your computer and use it in GitHub Desktop.
A sample script to delete datadog monitors
#
# Setup
# pip3 install datadog-api-client
#
# Command to run
# DD_SITE="datadoghq.com" DD_API_KEY=$DATADOG_API_KEY DD_APP_KEY=$DATADOG_APP_KEY python3 delete_datadog_monitors.py
# Sample monitor_list.txt. All monitor IDs are stored in new line.
#
# cat monitor_list.txt
# 200000
# 280926
# 250002
# 709372
from datadog_api_client import ApiClient, Configuration, exceptions
from datadog_api_client.v1.api.monitors_api import MonitorsApi
failed_monitors = {
"NotFound" : [],
"FailedToDelete" : []
}
configuration = Configuration()
with ApiClient(configuration) as api_client:
api_instance = MonitorsApi(api_client)
with open('monitor_list.txt') as monitor_list:
for each_monitor in monitor_list:
response = ""
try:
response = api_instance.delete_monitor(
monitor_id=int(each_monitor),
)
except exceptions.NotFoundException as e:
failed_monitors["NotFound"].append(each_monitor)
except Exception as e:
print(f'Failed to delete: {each_monitor}')
print(e)
failed_monitors["FailedToDelete"].append(each_monitor)
print(failed_monitors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment