Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Automatic Resource Deletion

sahana
New Member

I want to delete the resources in GCP based on the tags at the end of the day automatically, and have to keep some resources which are important how can i do that one. Please suggest

Solved Solved
1 3 589
1 ACCEPTED SOLUTION

glen_yu
Google Developer Expert
Google Developer Expert

You're in luck as I happened to be working on some CF stuff lately anyway.  I'm not going to give you the full answer, but here's the python code to find and stop (commented out) instances in a particular project/zone with the 'save_money' label.  

 

You will have to learn how to turn this in to a CF.  The pip package you'll need is google-api-python-client==2.31.0

 

#! /usr/bin/env python3

# export GOOGLE_APPLICATION_CREDENTIALS='/path/to/credentials.json'

import json
import googleapiclient.discovery


compute = googleapiclient.discovery.build('compute', 'v1')

result = compute.instances().list(project='mygcpproject-123', zone='northamerica-northeast2-a').execute()
#pretty_json_output = json.dumps(result, indent=2)
#print(pretty_json_output)

#print(len(result)) #returns: id, items, selfLink, kind
vms_list=result['items']
num_vms=len(vms_list)

# loop through your instances list and find the ones with 'save_money' in the output (which is your label's key name -- it doesn't matter what the value is)
for i in range(num_vms):
    if "save_money" in json.dumps(vms_list[i]):
        print("stopping {}".format(vms_list[i]['name']))
        #compute.instances().stop(project='mygcpproject-123', zone='northamerica-northeast2-a', instance=vms_list[i]['name']).execute()
    else:
        print('no')

 

 

Here's the list of API libraries if you're not a Python fan, and here's the Python API Client documentation for instances 

 

And here's now to use Cloud Scheduler to invoke Cloud Functions 

 

 

View solution in original post

3 REPLIES 3

glen_yu
Google Developer Expert
Google Developer Expert

I'm not sure about *all* resources, but for Compute Engines at least I would write a Cloud Function to programmatically delete based on the tags/labels.  Then use Cloud Scheduler to trigger said CF nightly.

Can you explain how you did that? I'm looking to do something similar. Thanks in advanced!

glen_yu
Google Developer Expert
Google Developer Expert

You're in luck as I happened to be working on some CF stuff lately anyway.  I'm not going to give you the full answer, but here's the python code to find and stop (commented out) instances in a particular project/zone with the 'save_money' label.  

 

You will have to learn how to turn this in to a CF.  The pip package you'll need is google-api-python-client==2.31.0

 

#! /usr/bin/env python3

# export GOOGLE_APPLICATION_CREDENTIALS='/path/to/credentials.json'

import json
import googleapiclient.discovery


compute = googleapiclient.discovery.build('compute', 'v1')

result = compute.instances().list(project='mygcpproject-123', zone='northamerica-northeast2-a').execute()
#pretty_json_output = json.dumps(result, indent=2)
#print(pretty_json_output)

#print(len(result)) #returns: id, items, selfLink, kind
vms_list=result['items']
num_vms=len(vms_list)

# loop through your instances list and find the ones with 'save_money' in the output (which is your label's key name -- it doesn't matter what the value is)
for i in range(num_vms):
    if "save_money" in json.dumps(vms_list[i]):
        print("stopping {}".format(vms_list[i]['name']))
        #compute.instances().stop(project='mygcpproject-123', zone='northamerica-northeast2-a', instance=vms_list[i]['name']).execute()
    else:
        print('no')

 

 

Here's the list of API libraries if you're not a Python fan, and here's the Python API Client documentation for instances 

 

And here's now to use Cloud Scheduler to invoke Cloud Functions