Skip to content

Instantly share code, notes, and snippets.

@arnecls
Forked from jeffadams/delete_custom_metrics.py
Last active February 4, 2022 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnecls/9908e7dbe168fec3a8b062fcee50af75 to your computer and use it in GitHub Desktop.
Save arnecls/9908e7dbe168fec3a8b062fcee50af75 to your computer and use it in GitHub Desktop.
script to delete all custom metrics from a stackdriver account
#!/usr/bin/env python
from google.cloud import monitoring_v3
'''
Requires you to be logged in via `gcloud auth application-default login`.
'''
project_id = 'my-project-id' # change project id here
metrics_prefix = 'custom.googleapis.com'
client = monitoring_v3.MetricServiceClient()
project = client.project_path(project_id)
# Delete ALL custom metrics from this project.
all = client.list_metric_descriptors(project, filter_='metric.type=starts_with("'+metrics_prefix+'")')
for a in all:
print("deleting: "+a.type)
client.delete_metric_descriptor(a.name)
print("done")
@adekoyadapo
Copy link

Update this seems to be working more recently

#!/usr/bin/env python

from google.cloud import monitoring_v3

'''
Requires you to be logged in via `gcloud auth application-default login`.
'''

project_id = 'my-project-id' # change project id here
metrics_prefix = 'custom.googleapis.com'

client = monitoring_v3.MetricServiceClient()
project = client.common_project_path(project_id)
project_name = f"projects/{project_id}"
request = dict(
      name=project_name,
      filter=f'metric.type = starts_with("'+metrics_prefix+'")'
      )
# Delete ALL custom metrics from this project.
all = client.list_metric_descriptors(request=request)
for a in all:
    print("deleting: "+a.type)
    client.delete_metric_descriptor(name=a.name)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment