This document describes how to delete virtual machine (VM) instances.
If you no longer need a VM, delete it to stop incurring charges for the VM and its attached resources. If you want to preserve an attached resource, do one or more of the following before deleting the VM:
To preserve a GPU attached to an N1 VM that you want to delete, remove the GPU from the VM.
To preserve a Persistent Disk, do one of the following:
To preserve a boot disk, detach the boot disk from the VM.
To preserve a boot or non-boot disk, set the disk's auto-delete setting to
false
.
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 VM,
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 VM. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to delete a VM:
-
compute.instances.delete
on the VM -
To force the deletion of an attached Persistent 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 VM, Google Cloud no longer bills you for the VM and its attached resources unless any of the following situations apply:
If you delete a VM that is hosted on a sole-tenant node, you continue paying for the sole-tenant node itself instead of the individual VMs hosted on the node.
If you have a committed use discount, 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 VM, you continue incurring charges for those resources until you delete them. For example, if you delete a VM but preserve the Persistent Disk volumes attached to it, you continue incurring charges for the disks.
For more information, see VM instances pricing.
Process for deleting a VM
When deleting a VM, Compute Engine does the following:
Compute Engine sends the ACPI shutdown signal to the VM, and then it sets the VM state to
STOPPING
within a few seconds.Compute Engine starts a clean shutdown of the operating system (OS) within the VM. The shutdown period lasts as follows based on the VM type:
Preemptible VMs: Preemptible VMs have a shutdown period of 30 seconds, which is the same length of the preemption process.
Other VM types: Any other VM types have a shutdown period of at least 90 seconds.
At the end of the shutdown period, Compute Engine permanently deletes the VM and its attached resources. If you configured a Persistent Disk to be preserved when deleting the VM it's attached to, Compute Engine preserves the disk upon VM deletion.
Delete VMs
To delete one or more VMs, see one of the following sections from this document:
To delete VMs and all of their attached resources (default), see Delete VMs and all attached resources.
To delete VMs but preserve their attached Persistent Disk volumes, see Delete VMs and preserve Persistent Disk volumes.
Delete VMs and all attached resources
When you delete a VM, Compute Engine deletes the VM and all of its attached resources by default. However, if you configured a Persistent Disk to be preserved upon VM deletion, you can override this setting and delete both the VM and the disk using the gcloud CLI.
To delete multiple VMs simultaneously, use either the Google Cloud console or, for VMs located in the same zone, the gcloud CLI. Otherwise, to delete a single VM, use any method described in this section.
Console
To delete one or more VMs, do the following:
In the Google Cloud console, go to the VM instances page.
Select the VMs that you want to delete.
Click
Delete, and then click Delete again to confirm.
gcloud
To delete one or more VMs, use the
gcloud compute instances delete
command.
gcloud compute instances delete VM_NAMES \
--zone=ZONE
Replace the following:
VM_NAMES
: a whitespace-separated list of names of VMs that are located in the same zone; for example,vm-01 vm-02 vm-03
.ZONE
: the zone of the VM.
Optionally, to force the deletion of the Persistent Disk volumes attached
to one or more VMs, include the --delete-disks
flag.
gcloud compute instances delete VM_NAMES \
--delete-disks=DELETE_DISK_TYPE \
--zone=ZONE
Replace DELETE_DISK_TYPE
with the type of attached
Persistent Disk volumes to delete. Specify one of the following values:
To delete all types of disks:
all
.To delete only boot disks:
boot
.To delete only non-boot disks:
data
.
C#
Go
Java
Node.js
PHP
Python
Ruby
REST
To delete a VM, make a DELETE
request to the
instances delete
method.
DELETE https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME
Replace the following:
PROJECT_ID
: the ID of the project where the VM is located.ZONE
: the zone of the VM.VM_NAME
: the VM name.
Delete VMs and preserve Persistent Disk volumes
By default, deleting a VM deletes the VM and its attached resources. However, when you delete a VM using the gcloud CLI, you can specify to preserve the attached boot disks, non-boot disks, or both regardless of the disks' auto-delete settings.
To delete one or more VMs located in the same zone while preserving their
attached Persistent Disk volumes, use the
gcloud compute instances delete
command
with the --keep-disks
flag.
gcloud compute instances delete VM_NAMES \
--keep-disks=KEEP_DISK_TYPE \
--zone=ZONE
Replace the following:
VM_NAMES
: a whitespace-separated list of names of VMs that are located in the same zone; for example,vm-01 vm-02 vm-03
.KEEP_DISK_TYPE
: the types of attached Persistent Disk volumes to preserve. Specify one of the following values:To preserve all types of disks:
all
.To preserve only boot disks:
boot
.To preserve only non-boot disks:
data
.
ZONE
: the zone where the VMs are located.
What's next
Learn more about a VM life cycle.
Learn more about the costs incurred for stopped VMs.
Learn how to limit the runtime of a VM.