This document explains how to delete Compute Engine instances. 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.
If you want to preserve an attached resource, then do one or more of the following before deleting the instance:
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 disk's auto-delete setting to
false
.
To preserve the data of a Local SSD disk, copy the disk's data to a persistent storage option.
To reuse the static external IP address of an instance, unassign the IP address.
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, Google Cloud no longer bills you 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.
Delete instances
When you delete a compute instance, Compute Engine first stops the instance if it's running. Additionally, you can choose to delete or preserve the attached disks as follows:
Delete instances and all attached resources
Use this option to delete one or more instances simultaneously, and delete, or force the deletion of, any attached disks.
Delete instances and preserve disks
Use this option to delete one or more instances simultaneously, but preserve the attached disks.
Delete instances and all attached resources
When you delete a compute instance, Compute Engine deletes the instance and all of its attached resources by default. However, if you configured an attached disk to be preserved upon instance deletion, you can override this setting and force its deletion when deleting instances using the gcloud CLI.
You can delete multiple instances simultaneously or individual instances. For multiple instances, use the Google Cloud console or, for instances located in the same zone, the gcloud CLI. For individual instances, select any 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, and then click Delete again to confirm.
gcloud
To delete one or more instances in a single zone, use the
gcloud compute instances delete
command:
gcloud compute instances delete INSTANCE_NAMES \
--zone=ZONE
Replace the following:
INSTANCE_NAMES
: a whitespace-separated list of names of instances—for example,instance-01 instance-02 instance-03
.ZONE
: the zone where the instances are located.
Optionally, 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
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.
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 a single 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 whitespace-separated list of names of instances—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.