How to delete a GCP Resource Folder programatically?

226 Views Asked by At

I am trying to create a cron service that cleans up (removes billing and deletes) a bunch of projects and resource folders on GCP for a development team. Ideally, I would like to use cloud functions to do this as its lightweight, however, I can't seem to find any client libraries that would allow for FOLDER deletion. Has anyone been able to find any client libraries to do this?

The other options I am considering are:

1

There are 1 best solutions below

1
On

You can use the discovery API. We use it in production (only for search and then attach project to a folder)

An example of our python code

from googleapiclient import discovery 
from oauth2client.client import GoogleCredentials

#Use GCF Service Account credentials for all APIs call
CREDENTIALS = GoogleCredentials.get_application_default()

SERVICE_RESOURCE_MANAGER_FOLDER = discovery.build('cloudresourcemanager', 'v2', 
                                                    credentials=CREDENTIALS)

# Example for a search
    request = SERVICE_RESOURCE_MANAGER_FOLDER.folders().search(body=body)
    response = request.execute()

Use the delete operation if you want to do this.