Quick cleaning of GCP projects from the command line

Emanuele Pecorari
1 min readMay 21, 2022

The GCP user interface is quite cool and really usable but some operations can be done without going on it and often, it can be faster.

Let’s see for example how quickly we can see the projects in our GCP account and choose some to delete

List the projects

First, it’s possible to have an overview of the projects with the following command (supposing the authentication for the GCP CLI is already one):

gcloud projects list

that will give something like this:

PROJECT_ID          NAME            PROJECT_NUMBER
mercurial-idiom-728 LoopbackPush YYYYYYYYYYYY
voiceroutertest VoiceRouterTest ZZZZZZZZZZZZ

Delete a project

Now, using the PROJECT_NUMBER value, we can delete a project:

gcloud projects delete <PROJECT_NUMBER>

A confirmation will be asked.

Recover a delete project

In case we delete a project mistakenly, there are 30 days to have it back. It’s possible to list the projects for which the deletion has been requested:

gcloud projects list — filter=’lifecycleState:DELETE_REQUESTED’

and then, using again the PROJECT_NUMBER:

gcloud projects undelete <PROJECT_NUMBER>

the project can be recovered.

--

--

Emanuele Pecorari

Cloud Architect and Tech Product Owner. Soccer player and coach in the free time.