Is it possible for an instance to self destroy

2,246 views
Skip to first unread message

Zhuyi Xue

unread,
Feb 25, 2016, 7:03:15 PM2/25/16
to gce-discussion
Is it possible for an instance to destroy to delete itself? For example, can I write some code to do some task and upload the results to GCS, then I'd like to instance to be deleted right away.

Faizan (Google Cloud Support)

unread,
Feb 26, 2016, 11:52:21 AM2/26/16
to gce-dis...@googlegroups.com
Hello Zhuyi,

Currently we don't have an option to delete the instance automatically. With that said, you can delete your instance by either using the GCE instance delete API, gcloud or developer console. You can automate this process by using GCE API or gcloud commands in a script.

You can also take a look at Preemptible VM to see if they suit your needs. These VMs are short-lived GCE instances suitable for batch jobs and fault tolerant workloads. For more information you can refer to this link.

I hope that helps.

Faizan

Michael Basilyan

unread,
Feb 29, 2016, 5:40:03 PM2/29/16
to Faizan (Google Cloud Support), gce-discussion
I had to do something like this earlier. Here's what I did: 
  1. Make sure your VM is created with the right scope for your service accounts: https://cloud.google.com/compute/docs/authentication#using

  2. Once you need to shutdown you can run:
    gcloud compute instances delete $myName --zone=$myZone --quiet

  3. You can get the name of the VM just using the hostname command in Linux: myName=$(hostname)

  4. You can also parse the zone out of the metadata, like this:

    # Get the zone
    zoneMetadata=$(curl "http://metadata.google.internal/computeMetadata/v1/instance/zone" -H "Metadata-Flavor:Google")
    # Split on / and get the 4th element to get the actual zone name
    IFS=$'/'
    zoneMetadataSplit=($zoneMetadata)
    myZone="${zoneMetadataSplit[3]}"


    That should more-or-less work. You may not even need to specify the --zone. gcloud might just default to the right zone (Not 100% sure). 

On Fri, Feb 26, 2016 at 8:52 AM, 'Faizan (Google Cloud Support)' via gce-discussion <gce-dis...@googlegroups.com> wrote:
Hello Zhuyi,

If you wish to delete an instance once you have completed the task you can do so by either using the GCE instance delete API, gcloud or developer console. You can automate this process by using GCE API or gcloud commands in a script.

You can also take a look at Preemptible VM to see if they suit your needs. These VMs are short-lived GCE instances suitable for batch jobs and fault tolerant workloads. For more information you can refer to this link.

I hope that helps.

Faizan

--
© 2014 Google Inc. 1600 Amphitheatre Parkway, Mountain View, CA 94043
 
Email preferences: You received this email because you signed up for the Google Compute Engine Discussion Google Group (gce-dis...@googlegroups.com) to participate in discussions with other members of the Google Compute Engine community and the Google Compute Engine Team.
---
You received this message because you are subscribed to the Google Groups "gce-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gce-discussio...@googlegroups.com.
To post to this group, send email to gce-dis...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gce-discussion/7dd0f34d-94fe-4e31-b887-58d90228292e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Michael Basilyan

unread,
Feb 29, 2016, 5:56:52 PM2/29/16
to Faizan (Google Cloud Support), gce-discussion
In fact, this seems to work fine:

gcloud compute instances delete $(hostname) --quiet

You still have to make sure your VM is created with the right scope for your service accounts: https://cloud.google.com/compute/docs/authentication#using

O C

unread,
Jan 8, 2018, 4:07:55 PM1/8/18
to gce-discussion
What is this then ? https://cloud.google.com/compute/docs/reference/latest/instances/delete

That's why I don't pay for Bronze age support. 

O C

unread,
Jan 8, 2018, 4:07:55 PM1/8/18
to gce-discussion
here:       https://cloud.google.com/compute/docs/reference/latest/instances/delete#examples

where it says string instance = "my-instance";  // TODO: Update placeholder value.

//swap out "my-instance" for string hostName = System.Environment.MachineName
Reply all
Reply to author
Forward
0 new messages