This document explains how to stop or restart a Compute Engine instance. To learn more about the effects of stopping an instance, as well as the differences between suspending, stopping, or resetting an instance, see Suspend, stop, or reset Compute Engine instances.
Stopping an instance is useful when you no longer use it, or to modify its properties—for example, to change its machine type, or remove any attached and mounted disks. After you stop the instance, you can do the following:
Restart it to resume your workload.
Delete it if you no longer need it.
To automate stopping or restarting an instance, see the following instead:
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.
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.
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 stop and restart 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 stop and restart a compute instance. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to stop and restart a compute instance:
-
To stop an instance:
compute.instances.stop
on the instance -
To stop an instance from within the guest OS:
compute.instances.setMetadata
on the instance if it uses instance-level public SSH keys. -
To restart an instance:
compute.instances.start
on the instance -
To restart an instance that uses encryption keys:
compute.instances.startWithEncryptionKey
on the instance
You might also be able to get these permissions with custom roles or other predefined roles.
Stop an instance
When you stop a compute instance and Compute Engine changes its state to
TERMINATED
, the instance retains its attached resources, configuration
settings, internal IP addresses, MAC addresses, and metadata. However, the
instance loses its in-memory data and application state. If you need to retain
these, then
suspend the instance instead.
You can stop an instance using the following methods. The method you choose depends on whether the instance has Local SSD disks attached, and how you want to handle shutdown scripts:
Stop an instance without Local SSD disks
You can stop one or more instances simultaneously that don't have any Local SSD disks attached.
Stop an instance with Local SSD disks
When stopping one or more instances that have Local SSD disks attached, you can choose to discard or preserve (Preview) the data on those disks.
Stop an instance from the guest OS
You can stop an instance from within its guest OS. This is useful if you're connected to the instance or have shutdown scripts that need to run before the instance stops. However, unless you manually back up data from any Local SSD disks to persistent storage volume, stopping an instance from within its guest OS discards any data on the attached Local SSD disks.
After stopping an instance, you can restart the instance when needed, or delete the instance if you no longer need it.
Stop an instance without Local SSD disks
You can stop multiple compute instances simultaneously or individual instances. For multiple instances, use the Google Cloud console or, for instances located in the same zone, the Google Cloud CLI. For individual instances, select any of the following options:
Console
In the Google Cloud console, go to the VM instances page.
Select the running instances to stop.
Click
Stop, and then click Stop to confirm.
gcloud
To stop one or more instances in a single zone, use the
gcloud compute instances stop
command:
gcloud compute instances stop 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.
Go
Java
Node.js
PHP
Python
REST
To stop an instance, make a POST
request to the
instances.stop
method:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/stop
Replace the following:
INSTANCE_NAME
: the name of the instance.PROJECT_ID
: the ID of the project where the instance is located.ZONE
: the zone where the instance is located.
Stop an instance with Local SSD disks
Based on how many compute instances you want to stop simultaneously and whether you need to preserve the data of their attached Local SSD disks, do the following:
To preserve the data of the Local SSD disks attached to an instance, stop the instance using the gcloud CLI or REST API.
To stop multiple instances simultaneously, use the Google Cloud console or, for instances located in the same zone, the gcloud CLI.
To stop one or more instances that have Local SSD disks attached, select one of the following options:
Console
In the Google Cloud console, go to the VM instances page.
Select the running instances to stop.
Click
Stop, and then click Stop to confirm.
gcloud
When stopping one or more instances in a single zone that have Local SSD disks attached, specify whether to discard or preserve Local SSD data as follows:
To discard Local SSD data, use the
gcloud compute instances stop
command with the--discard-local-ssd=true
flag:gcloud compute instances stop INSTANCE_NAMES \ --discard-local-ssd=true \ --zone=ZONE
To preserve Local SSD data, use the
gcloud beta compute instances stop
command with the--discard-local-ssd=false
flag:gcloud beta compute instances stop INSTANCE_NAMES \ --discard-local-ssd=false \ --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.
REST
When stopping an instance that has Local SSD disks attached, specify whether to discard or preserve Local SSD data as follows:
To discard Local SSD data, make a
POST
request to theinstances.stop
method. In the request URL, include thediscardLocalSsd
query parameter set totrue
:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/stop?discardLocalSsd=true
To preserve Local SSD data, make a
POST
request to thebeta.instances.stop
method. In the request URL, include thediscardLocalSsd
query parameter set tofalse
:POST https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/stop?discardLocalSsd=false
Replace the following:
PROJECT_ID
: the ID of the project where the instance is located.ZONE
: the zone where the instance is located.INSTANCE_NAME
: the name of the instance.
Stop an instance from the guest OS
If you stop a compute instance that has Local SSD disks attached, then stopping the instance from within its guest OS discards the Local SSD data. If you want to preserve the Local SSD data, then, before stopping the instance, you must manually copy the data to a persistent storage option, such as attached Google Cloud Hyperdisk or Persistent Disk volumes, or even Cloud Storage.
To stop an instance from within the guest OS, select one of the following options:
Linux
If you haven't already, then connect to the instance.
To stop the instance, select one of the following methods:
For a clean shutdown that allows the instance to run shutdown scripts before shutting down the guest OS, run the following command:
sudo shutdown -h now
Otherwise, to force a shutdown, run the following command:
sudo poweroff
Windows
If you haven't already, then connect to the instance using one of the following methods:
To stop the instance, select one of the following methods:
For a clean shutdown that allows the instance to run shutdown scripts before shutting down the guest OS, run the following command:
shutdown /s
Otherwise, to force a shutdown, run the following command:
shutdown /f
Restart an instance
You can restart a compute instance that has been fully stopped, which is when
the instance state is TERMINATED
.
If you chose to preserve the data of your Local SSD disks when stopping the instance, then you might need to remount the Local SSD disks after restarting it. For more information about how to mount Local SSD disks, see Format and mounting a Local SSD device.
To restart an instance, use one of the following methods based on whether the instance has encrypted disks attached:
If your instance doesn't have encrypted disks attached, then see Restart an instance without encrypted disks.
Otherwise, see Restart an instance with encrypted disks.
Restart an instance without encrypted disks
You can restart multiple compute 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 one or more instances.
Click
Start / Resume.
gcloud
To restart one or more instances in a single zone, use the
gcloud compute instances start
command:
gcloud compute instances start 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.
Go
Java
Node.js
PHP
Python
REST
To restart an instance, make a POST
request to the
instances.start
method:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/start
Replace the following:
INSTANCE_NAME
: the name of the instance to restart.PROJECT_ID
: the ID of the project where the instance is located.ZONE
: the zone where the instance is located.
Restart an instance with encrypted disks
When you restart a stopped compute instance that has attached disks that were encrypted using customer-supplied encryption keys, you must supply the encryption key information.
To restart multiple instances simultaneously, use the Google Cloud console or, for instances located in the same zone, the gcloud CLI. Otherwise, to restart a single instance, select any of the following options:
Console
In the Google Cloud console, go to the VM instances page.
Select the instances to restart.
Click
Start / Resume.Specify encryption keys for each of the encrypted disks that are attached to the instances, and then click Start.
gcloud
To restart one or more instances in a single zone that use encrypted disks,
use the
gcloud compute instances start
command
with the --csek-key-file
flag. If you're using an RSA-wrapped key, then
use the
gcloud beta compute instances start
command
with the --csek-key-file
flag instead:
gcloud compute instances start INSTANCE_NAMES \
--csek-key-file=ENCRYPTION_KEY_FILE \
--zone=ZONE
Replace the following:
INSTANCE_NAMES
: a whitespace-separated list of names of instances—for example,instance-01 instance-02 instance-03
.ENCRYPTION_KEY_FILE
: the relative path to the JSON file that contains the customer-supplied encryption key. You can only restart multiple instances simultaneously if the instances use the same customer-supplied encryption key.ZONE
: the zone where the instances are located.
Go
Java
Node.js
PHP
Python
REST
To restart an instance that uses encrypted disks, make a POST
request to
the
instances.startWithEncryptionKey
method:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/startWithEncryptionKey
{
"disks": [
{
"source": "DISK_URL",
"diskEncryptionKey": {
"ENCRYPTION_TYPE": "ENCRYPTION_KEY"
}
}
]
}
Replace the following:
PROJECT_ID
: the ID of the project where the instance is located.ZONE
: the zone where the instance is located.INSTANCE_NAME
: the name of the instance.DISK_URL
: the resource URL corresponding to the full resource name of the attached disk that is encrypted with a customer-supplied encryption key.ENCRYPTION_TYPE
: the type of disk encryption that you're using, which can be one of the following:rawKey
,kmsKeyName
, orrsaEncryptedKey
. If you use thersaEncryptedKey
type, then make aPOST
request to thebeta.instances.startWithEncryptionKey
method.ENCRYPTION_KEY
: the encryption key used to encrypt the persistent disks attached to the instance.rawKey
orrsaEncryptedKey
keys must be base64-encoded. Additionally, to prepare arsaEncryptedKey
key, see RSA key wrapping.