Delete all resources in a Google Cloud Project

Viewed 7580

One of my project contains many resources, which are created from different sources, means some from Deployment Manager API and some from Console by users.I need to delete all resources without deleting(shutting down) the project.In this case, is there any API endpoints which can delete all resources in this project including both created from Deployment Manager and Console?

7 Answers

I wrote Safe Scrub to do exactly that: Clean up resources from across Google Cloud, including GCE, AppEngine, GKE, CloudSQL, and more.

It has a focus on safety. For example, it does not actually delete any resources, but rather generates a script (a list of delete commands) that you can review and run.

Feedback is appreciated.

Actually you can do this. Simply go to "IAM & Admin" -> "Manage Resources". This will take you to a management screen for your projects and you can delete them, see the billing, etc.

I don't think something like that exists, and it would be pretty dangerous if someone could just click on a button and shutdown/delete everything in a project, if you do a mistake with this you are in a position I don't want to think about.

A way to do what you want is something like a bash script that would use gcloud commands to delete what you need to delete.

Someone already tried but didn't go very far: https://github.com/enxebre/bazooka

Like night-gold said in the First Answer, right now there is no API endpoint to delete all resources inside a project.I discussed with a Google Cloud Engineer, and reply from him was:

"You will be able to delete the Deployment Manager resource by deleting the deployment itself. The resources created by a user through the console must be addressed individually. You can automate deleting the resources made by a user. Some suggested tools for accomplishing this would be to use Chef, Puppet, Ansible, Terraform, &/or shell scripts."

You can use Deployment API's list method to gather all deployments then the delete method which will remove the deployments and all of the resources.

With the appropriate IAM config, here's how to delete all Endpoints for a given project :

#!/bin/bash
# 
# A script to delete all endpoints for a given project
# Usage: ./delete-endpoints.sh project-id

PROJECT=$1

ENDPOINTS=($(gcloud --project=${PROJECT} endpoints services list --format="value(serviceName)"))
for i in "${ENDPOINTS[@]}"
do
        gcloud -q endpoints services delete $i --async
done

Similar scripts can delete all kinds of resources using gcloud

In my project, we are using cats-love-money which is a simple script to remove pricey GCP resources (composers, GKEs, Dataprocs). By default, we remove everything older than 24h and without please-do-not-kill-me label.

It can be deployed on schedule using cloud functions and scheduler. It saves us a lot of cash.

Related