This document explains how to manually stop or restart a Compute Engine instance. Alternatively, to automate when a compute instance stops or restarts, see the following:
It's possible to manually stop a compute instance. After a stop operation
completes and the state of the compute instance transitions to TERMINATED, you
can either restart the compute instance to resume your workload, or permanently
delete the compute instance if you no longer need it. Manually stopping a
compute instance is useful when you want to do the following:
Save costs while keeping your configurations: you no longer want to run workloads on the compute instance. However, you want to retain the compute instance and its attached disks, configuration settings, IP addresses, MAC addresses, and metadata. If you also need to retain in-memory data and application state, then suspend the compute instance instead.
Update properties: you want to modify one or more properties that require you to stop and restart the compute instance before the changes can take effect—for example, you want to change machine type or detach a disk. For more information, see Properties that you can update.
Limitations
When you stop a compute instance, the following limitations apply:
You can only stop or restart a compute instance if the state of the compute instance allows it.
You can only restart a compute instance if there are sufficient resources in the zone where the compute instance exists.
You can't restart TPU compute instances. After you stop TPU compute instances, you can only delete them.
Before you begin
-
If you haven't already, set up authentication.
Authentication verifies your identity 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. After installation, initialize the Google Cloud CLI by running the following command:
gcloud initIf you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- 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.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
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.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
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.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
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.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
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.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
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.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
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 or 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 or 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 or restart a compute instance:
-
To stop a compute instance:
compute.instances.stopon the compute instance -
To stop a compute instance from the guest OS:
compute.instances.setMetadataon the compute instance if it uses instance-level public SSH keys. -
To restart a compute instance:
compute.instances.starton the compute instance -
To restart a compute instance that uses encryption keys:
compute.instances.startWithEncryptionKeyon the compute instance
You might also be able to get these permissions with custom roles or other predefined roles.
Stop a compute instance
To stop your compute instance, use one of the following methods. The best approach for your use case depends on whether you have Local SSD disks attached to your compute instance and how you want to handle any shutdown scripts:
Stop a compute instance from Google Cloud. You can stop one or more compute instances simultaneously from Google Cloud. If the compute instances have Local SSD disks attached, then you can choose whether to discard or preserve (Preview) Local SSD data. Any shutdown scripts in the compute instance must finish running before the compute instance stops. For instructions, see one of the following:
Stop a compute instance from the guest OS. This approach ensures your shutdown scripts finish running before the compute instance stops. If you've enabled graceful shutdown, then you can skip or end the graceful shutdown. However, stopping a compute instance from the guest OS discards data on any attached Local SSD disks unless you first back the data up to durable storage. For instructions, see Stop a compute instance from the guest OS.
Stop a compute instance with no Local SSD disks
To stop a compute instance that doesn't have Local SSD disks attached, review the following options:
If you've enabled graceful shutdown in the compute instance, then you can save costs by skipping or ending an ongoing graceful shutdown. To do so, use the Google Cloud console, gcloud CLI, or REST API.
To stop multiple compute instances simultaneously, use the Google Cloud console or, for compute instances that exist in the same zone, the gcloud CLI.
To stop one or more compute instances, select one of the following options:
Console
In the Google Cloud console, go to the VM instances page.
Select the running compute instances to stop.
Click Stop. If there is no Stop option, then click More actions, and then select Stop.
In the dialog that appears, do the following:
Optional: To stop the compute instances without gracefully shutting them down, or to end an ongoing graceful shutdown, select the Skip graceful shutdown (if applicable) checkbox.
To confirm, click Stop.
gcloud
To stop one or more compute instances that exist in the same zone, use the
gcloud compute instances stop command:
gcloud compute instances stop INSTANCE_NAMES \
--zone=ZONE
Replace the following:
INSTANCE_NAMES: a list of names of compute instances that you want to stop, separated by spaces. For example, specifyinstance-01 instance-02 instance-03.ZONE: the zone where the compute instances exist.
If you want to skip or end an ongoing graceful shutdown, then use the same
command with the --no-graceful-shutdown flag:
gcloud compute instances stop INSTANCE_NAMES \
--no-graceful-shutdown \
--zone=ZONE
Go
Java
Node.js
PHP
Python
REST
To stop a compute 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 compute instance.PROJECT_ID: the ID of the project where the compute instance exists.ZONE: the zone where the compute instance exists.
If you want to skip or end an ongoing graceful shutdown, then include the
noGracefulShutdown=true query parameter in the request URL:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop?noGracefulShutdown=true
Stop a compute instance with Local SSD disks
The best method to stop a compute instance that has Local SSD disks attached depends on your use case. Use one of the following options:
If you've enabled graceful shutdown in a compute instance, then you can save costs by skipping or ending an ongoing graceful shutdown. To do so, use the Google Cloud console, gcloud CLI, or REST API.
To preserve Local SSD data (excluding for machine types that use Titanium SSD), use the gcloud CLI or REST API.
To stop multiple compute instances simultaneously, use the Google Cloud console or, for compute instances that exist in the same zone, the gcloud CLI.
To stop one or more compute 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 compute instances to stop.
Click Stop. If there is no Stop option, click More actions, and then select Stop.
In the dialog that appears, do the following:
Optional: To stop the compute instances without gracefully shut them down, or end an ongoing graceful shutdown, select the Skip graceful shutdown (if applicable) checkbox.
To confirm, click Stop.
gcloud
When you stop one or more compute instances in the same 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 stopcommand with the--discard-local-ssd=trueflag:gcloud compute instances stop INSTANCE_NAMES \ --discard-local-ssd=true \ --zone=ZONETo preserve Local SSD data, use the
gcloud beta compute instances stopcommand with the--discard-local-ssd=falseflag:gcloud beta compute instances stop INSTANCE_NAMES \ --discard-local-ssd=false \ --zone=ZONE
Replace the following:
INSTANCE_NAMES: a list of names of compute instances that you want to stop, separated by spaces. For example, specifyinstance-01 instance-02 instance-03.ZONE: the zone where the compute instances exist.
If you want to skip or end an ongoing graceful shutdown, then include the
--no-graceful-shutdown flag in the command:
gcloud compute instances stop INSTANCE_NAMES \
--discard-local-ssd=DISCARD_LOCAL_SSD \
--no-graceful-shutdown \
--zone=ZONE
Replace DISCARD_LOCAL_SSD with true to discard the data
in the Local SSD disks, or false to preserve the data. If you specify
false, then you must use the
gcloud beta compute instances stop command.
REST
When you stop one or more compute instances in the same zone that have Local SSD disks attached, specify whether to discard or preserve Local SSD data as follows:
To discard Local SSD data, make a
POSTrequest to theinstances.stopmethod. In the request URL, include thediscardLocalSsdquery parameter set totrue:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/stop?discardLocalSsd=trueTo preserve Local SSD data, make a
POSTrequest to thebeta.instances.stopmethod. In the request URL, include thediscardLocalSsdquery 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 compute instance exists.ZONE: the zone where the compute instance exists.INSTANCE_NAME: the name of the compute instance.
If you want to skip or end an ongoing graceful shutdown, then include the
noGracefulShutdown=true query parameter in the request URL:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop?discardLocalSsd=DISCARD_LOCAL_SSD&noGracefulShutdown=true
Replace DISCARD_LOCAL_SSD with true to discard the data
in the Local SSD disks, or false to preserve the data. If you specify
false, then you must make a POST request to the
beta.instances.stop method.
Stop a compute instance from the guest OS
If a compute instance has Local SSD disks attached, then shutting down the guest OS automatically discards the Local SSD data. To preserve this data, manually copy the data to a persistent storage option before you stop the compute instance.
To stop a compute instance from the guest OS, select one of the following options:
Linux
If you haven't already, then connect to the compute instance.
To stop the compute instance from within its guest OS, select one of the following methods:
To cleanly shut down the guest OS only after all shutdown scripts have finished running, run the following command:
sudo shutdown -h nowTo force a shutdown of the guest OS, run the following command:
sudo poweroff
Windows
If you haven't already, then connect to the compute instance using one of the following methods:
To stop the compute instance from within its guest OS, select one of the following methods:
To cleanly shut down the guest OS only after all shutdown scripts have finished running, run the following command:
shutdown /sTo force a shutdown of the guest OS, run the following command:
shutdown /f
Restart a compute instance
You can restart a compute instance that has been fully stopped, which is when
the compute instance state is TERMINATED. If you opted to preserve Local SSD
data when you stopped your compute instance, then you might need to remount the
Local SSD disks after you restart the compute instance. For more information,
see
Format and mounting a Local SSD device.
To restart a compute instance, use one of the following methods. The best method for your use case depends on whether the compute instance has disks that were encrypted with customer-supplied encryption keys (CSEK):
Restart a compute instance that doesn't have CSEK-encrypted disks
To restart multiple compute instances simultaneously across different zones, use the Google Cloud console. For compute instances that exist in the same zone, you can use the gcloud CLI. Otherwise, for other restart scenarios, select one of the following options:
Console
In the Google Cloud console, go to the VM instances page.
Select the compute instances that you want to restart.
Click Start / Resume.
gcloud
To restart one or more compute instances that exist in the same zone, use
the
gcloud compute instances start command:
gcloud compute instances start INSTANCE_NAMES \
--zone=ZONE
Replace the following:
INSTANCE_NAMES: a list of names of compute instances that you want to restart, separated by spaces. For example, specifyinstance-01 instance-02 instance-03.ZONE: the zone where the compute instances exist.
Go
Java
Node.js
PHP
Python
REST
To restart a compute 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 compute instance.PROJECT_ID: the ID of the project where the compute instance exists.ZONE: the zone where the compute instance exists.
Restart a compute instance with CSEK-encrypted disks
When you restart a stopped compute instance that has attached disks that were encrypted by using customer-supplied encryption keys, you must supply the encryption key information.
To restart multiple compute instances simultaneously across different zones, use the Google Cloud console. For compute instances that exist in the same zone, you can use the gcloud CLI. Otherwise, for other restart scenarios, select one of the following options:
Console
In the Google Cloud console, go to the VM instances page.
Select the compute instances that you want to restart.
Click Start / Resume.
In the dialog that appears, do the following:
Specify encryption keys for each of the encrypted disks that are attached to the compute instances.
Click Start.
gcloud
To restart one or more compute instances that use encrypted disks that exist
in the same zone, 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 list of names of compute instances that you want to restart, separated by spaces. For example, specifyinstance-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 compute instances simultaneously if the compute instances use the same customer-supplied encryption key. The format of the file is described in Key file format.ZONE: the zone where the compute instances exist.
Go
Java
Node.js
PHP
Python
REST
To restart a compute 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 compute instance exists.ZONE: the zone where the compute instance exists.INSTANCE_NAME: the name of the compute 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 thersaEncryptedKeytype, then make aPOSTrequest to thebeta.instances.startWithEncryptionKeymethod.ENCRYPTION_KEY: the encryption key used to encrypt the persistent disks attached to the compute instance.rawKeyorrsaEncryptedKeykeys must be base64-encoded. Additionally, to prepare arsaEncryptedKeykey, see RSA key wrapping.