This document explains how to delete a Compute Engine instance. To learn more about the lifecycle of an instance, see Compute Engine instance lifecycle.
If you no longer need an instance, then delete it to stop incurring charges for the instance and its attached resources.
Before you begin
-
If you haven't already, then set up authentication.
Authentication is
the process by which your identity is verified for access to Google Cloud services and APIs.
To run code or samples from a local development environment, you can authenticate to
Compute Engine by selecting one of the following options:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
- Set a default region and zone.
C#
To use the .NET samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
For more information, see Set up authentication for a local development environment.
Go
To use the Go samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
For more information, see Set up authentication for a local development environment.
Java
To use the Java samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
For more information, see Set up authentication for a local development environment.
Node.js
To use the Node.js samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
For more information, see Set up authentication for a local development environment.
PHP
To use the PHP samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
For more information, see Set up authentication for a local development environment.
Python
To use the Python samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
For more information, see Set up authentication for a local development environment.
Ruby
To use the Ruby samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
For more information, see Set up authentication for a local development environment.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
-
Required roles
To get the permissions that you need to delete a compute instance,
ask your administrator to grant you the
Compute Instance Admin (v1) (roles/compute.instanceAdmin.v1
) IAM role on the project.
For more information about granting roles, see Manage access to projects, folders, and organizations.
This predefined role contains the permissions required to delete a compute instance. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to delete a compute instance:
-
compute.instances.delete
on the instance -
To force the deletion of an attached disk:
compute.disks.delete
on the disk
You might also be able to get these permissions with custom roles or other predefined roles.
Billing implications
After you delete a compute instance, you're no longer charged for the instance and its attached resources unless any of the following situations apply:
If you delete an instance that is hosted on a sole-tenant node, then you continue paying for the sole-tenant node itself instead of the individual instances hosted on the node.
If you have a committed use discount, then you continue paying for the resources you committed to, whether or not you use those resources.
If you preserve any resources that were attached to the instance, then you continue incurring charges for those resources until you delete them. For example, if you delete an instance but preserve the disks attached to it, then you continue incurring charges for the disks.
For more information, see VM instances pricing.
Preserve attached resources
In some cases, before you delete a compute instance, you might want to preserve one of its attached resources. You can preserve attached resources by doing the following:
To preserve a GPU attached to an N1 virtual machine (VM) instance, remove the GPU from the VM.
To preserve an attached disk, do one of the following:
To preserve a boot disk, detach the boot disk from the instance.
To preserve a boot or non-boot disk, set the auto-delete setting of the disk to
false
.
To preserve the data of a Local SSD disk, copy the data of the disk to a persistent storage option.
To reuse the static external IP address of an instance, unassign the IP address.
Delete instances
When you delete a compute instance, Compute Engine stops the instance before deleting it.
If you delete one or more instances simultaneously, then you must decide what happens to the attached disks:
Delete instances and all attached resources
This option deletes the instances and deletes, or forces the deletion of, boot, non-boot, or all attached disks.
Delete instances and preserve disks
This option deletes the instances but preserves boot, non-boot, or all attached disks for later use.
Delete instances and all attached resources
Depending on what you want to do when deleting a compute instance, use the following options:
If you configured an attached disk to be preserved upon instance deletion, then you can override this setting and force its deletion using the Google Cloud CLI.
If you've enabled graceful shutdown in the instance, then you can delete the instance without gracefully shutting it down or end an ongoing graceful shutdown using the Google Cloud console, gcloud CLI, or REST API.
To delete multiple instances simultaneously, use the Google Cloud console or, for instances located in the same zone, the gcloud CLI.
To delete one or more instances and all attached resources, select one of the following options:
Console
In the Google Cloud console, go to the VM instances page.
Select the instances that you want to delete.
Click
Delete.In the dialog, do the following:
Optional: To delete the instances without gracefully shut them down, or end an ongoing graceful shutdown, select the Skip graceful shutdown (if applicable) checkbox.
To confirm, click Delete.
gcloud
To delete one or more instances in the same zone, use the
gcloud compute instances delete
command:
gcloud compute instances delete INSTANCE_NAMES \
--zone=ZONE
Replace the following:
INSTANCE_NAMES
: a list of instance names separated by spaces—for example,instance-01 instance-02 instance-03
.ZONE
: the zone where the instances are located.
Optionally, you can do one or both of the following:
To force the deletion of the disks attached to one or more instances, include the
--delete-disks
flag:gcloud compute instances delete INSTANCE_NAMES \ --delete-disks=DELETE_DISK_TYPE \ --zone=ZONE
Replace
DELETE_DISK_TYPE
with one of the following values:To delete attached boot and non-boot persistent storage:
all
To delete only attached boot persistent storage:
boot
To delete only non-boot persistent storage:
data
If you've enabled graceful shutdown in one or more instances, then you can delete the instances without gracefully shutting them down, or manually end an ongoing graceful shutdown. To do so, use the
gcloud beta compute instances delete
command with the--no-graceful-shutdown
flag:gcloud beta compute instances delete INSTANCE_NAMES \ --no-graceful-shutdown \ --zone=ZONE
C#
Go
Java
Node.js
PHP
Python
Ruby
REST
To delete an instance, make a DELETE
request to the
instances delete
method:
DELETE https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME
Replace the following:
PROJECT_ID
: the ID of the project where the instance is located.ZONE
: the zone of the instance.INSTANCE_NAME
: the instance name.
Optionally, if you've enabled graceful shutdown in the instance, you can
delete the instances without gracefully shutting it down, or manually end an
ongoing graceful shutdown. To do so, make a DELETE
request to the
beta instances.delete
method.
In the request URL, include the noGracefulShutdown
query parameter set to
true
:
DELETE https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME?noGracefulShutdown=true
Delete instances and preserve disks
By default, deleting a compute instance deletes the instance and its attached resources. However, when you delete an instance using the gcloud CLI, you can specify to preserve the attached disks, regardless of their auto-delete settings.
To delete one or more instances in the same zone while preserving their attached
disks, use the
gcloud compute instances delete
command
with the --keep-disks
flag:
gcloud compute instances delete INSTANCE_NAMES \
--keep-disks=KEEP_DISK_TYPE \
--zone=ZONE
Replace the following:
INSTANCE_NAMES
: a list of instance names separated by spaces—for example,instance-01 instance-02 instance-03
.KEEP_DISK_TYPE
: specify one of the following values:To preserve attached boot and non-boot persistent storage:
all
To preserve only attached boot persistent storage:
boot
To preserve only attached non-boot persistent storage:
data
ZONE
: the zone where the instances are located.
Optionally, if you've enabled
graceful shutdown in one
or more instances, you can delete the instances without gracefully shutting them
down, or manually end an ongoing graceful shutdown. To do so, use the
gcloud beta compute instances delete
command
with the --no-graceful-shutdown
flag:
gcloud beta compute instances delete VM_NAMES \
--keep-disks=KEEP_DISK_TYPE \
--no-graceful-shutdown \
--zone=ZONE