Stay organized with collections Save and categorize content based on your preferences.
This document explains how to limit the run time of new or existing virtual machine (VM) instances and how to monitor the run time of those VMs. For managed instance groups (MIGs), see Limit the run time of a MIG instead.
When limiting the run time of a VM, you can schedule the VM to be automatically terminated (stopped or deleted) when it reaches a specific time limit (duration or time). Use time limits to help optimize temporary workloads—by automatically limiting VM run times, you can help minimize costs and free up quotas.
Note: GPU VMs that are configured to be automatically deleted after a predefined run time of 7 days or less can consume either preemptible or standard allocation quotas. This behavior is intended to help you improve the obtainability of allocation quota for temporary-but-uninterrupted workloads. For more information about this behavior, see GPU VMs and preemptible allocation quotas.To learn how to immediately stop a VM, see Stop or restart a VM. To learn how to immediately delete a VM, see Delete a VM.
Before you beginTo get the permissions that you need to limit the run time of 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.
You might also be able to get the required permissions through custom roles or other predefined roles.
Select the tab for how you plan to use the samples on this page:
ConsoleWhen you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloudInstall the Google Cloud CLI. After installation, initialize the Google Cloud CLI by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update
.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. After installation, initialize the Google Cloud CLI by running the following command:
gcloud init
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.
Limiting the run time of a VM has the following restrictions:
The automatic termination action must be either stop or delete. You cannot configure a VM to be automatically suspended when the time limit is reached.
The minimum time limit is 30 seconds and the maximum time limit is 120 days.
Note: If you want to schedule a VM to stop after more than 120 days, you can use instance schedules instead.Automatic termination might take up to 30 seconds longer than the duration or time you specify to begin stopping or deleting the VM.
You cannot use time limits with legacy preemptible VMs. Instead, use time limits with Spot VMs.
A VM's local SSD data cannot be preserved when the VM is automatically stopped due to a time limit. For more information, see Stop a VM with Local SSD.
The following sections describe how to configure a time limit while creating a new VM. You can specify the time limit, which is when you want a VM to automatically terminate, as either a duration (maxRunDuration
) or a time (terminationTime
).
When deciding the type of time limit for a VM, consider that the VM might be interrupted—for example, by a user request or by a host event. The behavior of a time limit varies based on the type of interruption and the type of time limit:
A VM's termination timestamp (terminationTimestamp
) is a read-only field that represents the planned time for automatic termination and is automatically defined by Compute Engine whenever a VM has a time limit and enters the RUNNING
state.
The termination timestamp is automatically cleared whenever a VM is stopped or suspended. However, a termination timestamp doesn't change when you reset or reboot the VM.
The termination timestamp is automatically redefined whenever the VM is restarted or resumed based on the type of time limit that you set:
A duration represents the total run time that you want for a VM. To create a VM that is automatically terminated after the VM runs for a specific duration, use the Google Cloud console, Google Cloud CLI, Terraform, or Compute Engine API.
ConsoleIn the Google Cloud console, go to the Create an instance page.
In the Availability policies section, expand VM provisioning model advanced settings.
Select the Set a time limit for the VM checkbox. The Time limit type field appears.
In the Time limit type field, select By hours (default) to specify the time limit as a duration. In the following field, enter the duration in hours.
In the On VM termination list, select what happens when the run time of the VM reaches the specified time limit:
Optional: Specify other VM options. For more information, see Creating and starting a VM instance.
To create and start the VM, click Create.
To create a VM from the gcloud CLI, use the gcloud compute instances create
command.
--max-run-duration
flag.--instance-termination-action
flag.--discard-local-ssds-at-termination-timestamp
flag:
TERMINATION_ACTION
) is stop (STOP
), you must include the --discard-local-ssds-at-termination-timestamp=true
flag.--discard-local-ssds-at-termination-timestamp
flag.gcloud compute instances create VM_NAME \ --max-run-duration=DURATION \ --instance-termination-action=TERMINATION_ACTION
Replace the following:
VM_NAME
: The name of the new VM.DURATION
: The duration you want this VM to run before being automatically terminated. Format the duration as the number of days, hours, minutes, and seconds followed by d
, h
, m
, and s
respectively. For example, specify 30m
for a duration of 30 minutes, or specify 1d2h3m4s
for a duration of 1 day, 2 hours, 3 minutes, and 4 seconds. The minimum duration is 30 seconds (30s
) and the maximum duration is 120 days (120d
).TERMINATION_ACTION
: The termination action for this VM, which can be either stop (STOP
) or delete (DELETE
). Whether this field is required or has a default value varies based on the VM's provisioning model:
--provisioning-model=SPOT
flag), the --instance-termination-action=TERMINATION_ACTION
flag is optional. If this flag is omitted, the default termination action is stop.--instance-termination-action=TERMINATION_ACTION
flag is required.For more information about other options you can specify when creating a VM, see Creating and starting a VM instance.
TerraformTo create a VM using Terraform, use the google_compute_instance
resource.
max_run_duration
argument.instance_termination_action
argument. You must include the instance_termination_action
argument unless you are creating a Spot VM (set the provisioning_model
argument to SPOT
), which defaults to stop (STOP
).on_instance_stop_action
argument:
instance_termination_action
argument) is stop (STOP
), you must set the on_instance_stop_action
argument to true (true
).on_instance_stop_action
argument.For more information, see the Terraform documentation for the google_compute_instance
resource.
To create a VM from the Compute Engine API, use the instances.insert
method. You must specify a name, machine type, and boot disk for the VM.
To create a VM that is automatically terminated after a specific duration, you must include the maxRunDuration
field. To specify the termination action, include the instanceTerminationAction
field, which is optional for Spot VMs.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID
/zones/ZONE
/instances { "machineType": "zones/ZONE/machineTypes/MACHINE_TYPE", "name": "VM_NAME
", "disks": [ { "initializeParams": { "sourceImage": "projects/IMAGE_PROJECT/global/images/IMAGE" }, "boot": true } ] "scheduling": { "maxRunDuration": { "seconds": DURATION }, "instanceTerminationAction": "TERMINATION_ACTION" }, }
Replace the following:
PROJECT_ID
: The project id of the project to create the VM in.ZONE
: The zone to create the VM in. The zone must also support the machine type to use for the new VM.MACHINE_TYPE
: The predefined or custom machine type for the new VM.VM_NAME
: The name of the new VM.IMAGE_PROJECT
: The project containing the image. For example, if you specify family/debian-10
as the image, specify debian-cloud
as the image project.IMAGE
: The image for the new VM. You can specify either a specific version of a public image or an image family. For example, if you specify family/debian-10
as the image and debian-cloud
as the image project, Compute Engine creates a VM from the latest version of the OS image in the Debian 10 image family.DURATION
: The duration in seconds that you want this VM to run before being automatically terminated. The minimum duration is 30 seconds (30s
) and the maximum duration is 120 days (120d
).TERMINATION_ACTION
: The termination action for this VM, which can be either stop (STOP
) or delete (DELETE
). Whether this field is required or has a default value varies based on the VM's provisioning model:
"provisioningModel": "SPOT"
field), the "instanceTerminationAction": "TERMINATION_ACTION"
field is optional. If this field is omitted, the default termination action is stop."instanceTerminationAction": "TERMINATION_ACTION"
field is required.For more information about the options you can specify when creating a VM, see Creating and starting a VM instance.
Set a timeA time represents the date, time, and timezone when you want a VM to be terminated. To create a VM that is automatically terminated at a specific time, use the Google Cloud console, Google Cloud CLI, or Compute Engine API.
ConsoleIn the Google Cloud console, go to the Create an instance page.
In the Availability policies section, expand VM provisioning model advanced settings.
Select the Set a time limit for the VM checkbox. The Time limit type field appears.
In the Time limit type field, select By date to specify the time limit as a time and date. In the following field, click date_range Select date and time and select the date, time, and timezone for the time limit.
In the On VM termination list, select what happens when the run time of the VM reaches the specified time limit:
Optional: Specify other VM options. For more information, see Creating and starting a VM instance.
To create and start the VM, click Create.
To create a VM from the gcloud CLI, use the gcloud compute instances create
command.
--termination-time
flag.--instance-termination-action
flag, which is optional for Spot VMs.--discard-local-ssds-at-termination-timestamp
flag:
TERMINATION_ACTION
) to stop (STOP
), you must include the --discard-local-ssds-at-termination-timestamp=true
flag.--discard-local-ssds-at-termination-timestamp
flag.gcloud compute instances create VM_NAME \ --termination-time=TIME \ --instance-termination-action=TERMINATION_ACTION
Replace the following:
VM_NAME
: The name of the new VM.TIME
: The time you want this VM to be automatically terminated. The time you specify must be at least 30 seconds in the future and at most 120 days in the future. Format the time as a RFC 3339 timestamp:
YYYY-MM-DDTHH:MM:SSOFFSET
Replace the following:
YYYY-MM-DD
: A date formatted as a 4-digit year, 2-digit month, and 2-digit day of the month separated by hyphens.HH:MM:SS
: A time formatted as a 2-digit hour using 24-hour time, 2-digit minute, and 2-digit second separated by colons.OFFSET
: The time zone formatted as an offset of Coordinated Universal Time (UTC). For example, to use Pacific Standard Time (PST), which is 8 hours earlier than UTC, specify -08:00
. Alternatively, to use no offset (UTC+0), specify Z
.TERMINATION_ACTION
: The termination action for this VM, which can be either stop (STOP
) or delete (DELETE
). Whether this field is required or has a default value varies based on the VM's provisioning model:
--provisioning-model=SPOT
flag), the --instance-termination-action=TERMINATION_ACTION
flag is optional. If this flag is omitted, the default termination action is stop.--instance-termination-action=TERMINATION_ACTION
flag is required.For more information about other options you can specify when creating a VM, see Creating and starting a VM instance.
RESTTo create a VM from the Compute Engine API, use the instances.insert
method. You must specify a name, machine type, and boot disk for the VM.
To create a VM that is automatically terminated at a specific time, you must include the terminationTime
field. To specify the termination action, include the instanceTerminationAction
field, which is optional for Spot VMs.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID
/zones/ZONE
/instances { "machineType": "zones/ZONE/machineTypes/MACHINE_TYPE", "name": "VM_NAME
", "disks": [ { "initializeParams": { "sourceImage": "projects/IMAGE_PROJECT/global/images/IMAGE" }, "boot": true } ] "scheduling": { "terminationTime": "TIME", "instanceTerminationAction": "TERMINATION_ACTION" }, }
Replace the following:
PROJECT_ID
: The project id of the project to create the VM in.ZONE
: The zone to create the VM in. The zone must also support the machine type to use for the new VM.MACHINE_TYPE
: The predefined or custom machine type for the new VM.VM_NAME
: The name of the new VM.IMAGE_PROJECT
: The project containing the image. For example, if you specify family/debian-10
as the image, specify debian-cloud
as the image project.IMAGE
: The image for the new VM. You can specify either a specific version of a public image or an image family. For example, if you specify family/debian-10
as the image and debian-cloud
as the image project, Compute Engine creates a VM from the latest version of the OS image in the Debian 10 image family.TIME
: The time you want this VM to be automatically terminated. The time you specify must be at least 30 seconds in the future and at most 120 days in the future. Format the time as a RFC 3339 timestamp:
YYYY-MM-DDTHH:MM:SSOFFSET
Replace the following:
YYYY-MM-DD
: A date formatted as a 4-digit year, 2-digit month, and 2-digit day of the month separated by hyphens.HH:MM:SS
: A time formatted as a 2-digit hour using 24-hour time, 2-digit minute, and 2-digit second separated by colons.OFFSET
: The time zone formatted as an offset of Coordinated Universal Time (UTC). For example, to use Pacific Standard Time (PST), which is 8 hours earlier than UTC, specify -08:00
. Alternatively, to use no offset (UTC+0), specify Z
.TERMINATION_ACTION
: The termination action for this VM, which can be either stop (STOP
) or delete (DELETE
). Whether this field is required or has a default value varies based on the VM's provisioning model::
"provisioningModel": "SPOT"
field), the "instanceTerminationAction": "TERMINATION_ACTION"
field is optional. If this field is omitted, the default termination action is stop."instanceTerminationAction": "TERMINATION_ACTION"
field is required.For more information about the options you can specify when creating a VM, see Creating and starting a VM instance.
Limit the run time of an existing VMYou can limit the run time of an existing VM by updating the VM's schedule. If you don't already understand how to configure settings for time limits, review the previous sections about how to limit the run time of a new VM first.
You can use the Google Cloud console, Google Cloud CLI, or Compute Engine API to update the VM's scheduling-related properties, as described in this section. This method requires that you first stop your VM, then update its properties, then restart it. Alternatively, if you want to simultaneously update additional VM properties and automatically stop and restart a VM, see Update instance properties instead.
ConsoleIn the Google Cloud console, go to the VM instances page.
In the Name column, click the name of the VM that you want to update.
From the VM instance details page, complete the following steps:
On the Edit instance page, complete the following steps:
In the Availability policies section, modify the Set a time limit for the VM checkbox and any fields below it as desired.
For more information about how to configure the properties for time limits, see limit the run time of a new VM.
To save your changes, click Save.
Optional: If you want to start running the VM now, click Start.
To update a VM's time limit by using the gcloud CLI, complete the following steps:
If the VM is running, stop the VM by using the gcloud compute instances stop
command:
gcloud compute instances stop VM_NAME
Replace VM_NAME
with the name of VM that you want to update.
Update the VM's time limit by using the gcloud compute instances set-scheduling
command.
gcloud compute instances set-scheduling VM_NAME \ --max-run-duration=DURATION \ --termination-time=TIME \ --instance-termination-action=TERMINATION_ACTION \ --discard-local-ssds-at-termination-timestamp=true
Configure the following flags:
--max-run-duration
flag or the --termination-time
flag to set the time limit as either a time or duration, respectively.--instance-termination-action
flag to set the termination action.TERMINATION_ACTION
) to stop (STOP
), you must include the --discard-local-ssds-at-termination-timestamp=true
flag. Otherwise, omit the --discard-local-ssds-at-termination-timestamp=true
flag.Then, replace the following:
VM_NAME
: The name of the VM that you want to update.DURATION
: The duration you want this VM to run before being automatically terminated. Format the duration as the number of days, hours, minutes, and seconds followed by d
, h
, m
, and s
respectively. For example, specify 30m
for a duration of 30 minutes, or specify 1d2h3m4s
for a duration of 1 day, 2 hours, 3 minutes, and 4 seconds. The minimum duration is 30 seconds (30s
) and the maximum duration is 120 days (120d
).TIME
: The time you want this VM to be automatically terminated. The time you specify must be at least 30 seconds in the future and at most 120 days in the future. Format the time as a RFC 3339 timestamp:
YYYY-MM-DDTHH:MM:SSOFFSET
Replace the following:
YYYY-MM-DD
: a date formatted as a 4-digit year, 2-digit month, and 2-digit day of the month separated by hyphens.HH:MM:SS
: a time formatted as a 2-digit hour using 24-hour time, 2-digit minute, and 2-digit second separated by colons.OFFSET
: the time zone formatted as an offset of Coordinated Universal Time (UTC). For example, to use Pacific Standard Time (PST), which is 8 hours earlier than UTC, specify -08:00
. Alternatively, to use no offset (UTC+0), specify Z
.TERMINATION_ACTION
: The termination action for this VM, which can be either stop (STOP
) or delete (DELETE
). Whether this field is required or has a default value varies based on the VM's provisioning model:
--provisioning-model=SPOT
flag), the --instance-termination-action=TERMINATION_ACTION
flag is optional. If this flag is omitted, the default termination action is stop.--instance-termination-action=TERMINATION_ACTION
flag is required.If you want the VM to start running, start the VM by using the gcloud compute instances start
command:
gcloud compute instances start VM_NAME
Replace VM_NAME
with the name of the VM.
To update a VM's time limit by using the Compute Engine API, complete the following steps:
If the VM is running, stop the VM by using the instances.stop
method:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop
Replace the following:
PROJECT_ID
: The project id of the project that contains the VM.ZONE
: The zone that contains the VM.VM_NAME
: The name of the VM that you want to update.Update the VM's scheduling
property, including fields for the VM's time limit, by using the instances.setScheduling
method. Include either the maxRunDuration
field or the terminationTime
field to set the time limit as either a duration or time, respectively:
To set a duration, use the following request:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID
/zones/ZONE
/instances/VM_NAME/setScheduling { "maxRunDuration": { "seconds": DURATION }, "instanceTerminationAction": "TERMINATION_ACTION" }
To set a time, use the following request:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID
/zones/ZONE
/instances/VM_NAME/setScheduling { "terminationTime": "TIME", "instanceTerminationAction": "TERMINATION_ACTION" }
Replace the following:
PROJECT_ID
: The project id of the project to create the VM in.ZONE
: The zone to create the VM in. The zone must also support the machine type to use for the new VM.MACHINE_TYPE
: The predefined or custom machine type for the new VM.VM_NAME
: The name of the new VM.IMAGE_PROJECT
: The project containing the image. For example, if you specify family/debian-10
as the image, specify debian-cloud
as the image project.IMAGE
: The image for the new VM. You can specify either a specific version of a public image or an image family. For example, if you specify family/debian-10
as the image and debian-cloud
as the image project, Compute Engine creates a VM from the latest version of the OS image in the Debian 10 image family.DURATION
: The duration in seconds that you want this VM to run before being automatically terminated. The minimum duration is 30 seconds (30s
) and the maximum duration is 120 days (120d
).TIME
: The time you want this VM to be automatically terminated. The time you specify must be at least 30 seconds in the future and at most 120 days in the future. Format the time as a RFC 3339 timestamp:
YYYY-MM-DDTHH:MM:SSOFFSET
Replace the following:
YYYY-MM-DD
: A date formatted as a 4-digit year, 2-digit month, and 2-digit day of the month separated by hyphens.HH:MM:SS
: A time formatted as a 2-digit hour using 24-hour time, 2-digit minute, and 2-digit second separated by colons.OFFSET
: The time zone formatted as an offset of Coordinated Universal Time (UTC). For example, to use Pacific Standard Time (PST), which is 8 hours earlier than UTC, specify -08:00
. Alternatively, to use no offset (UTC+0), specify Z
.TERMINATION_ACTION
: The termination action for this VM, which can be either stop (STOP
) or delete (DELETE
). Whether this field is required or has a default value varies based on the VM's provisioning model:
"provisioningModel": "SPOT"
field), the "instanceTerminationAction": "TERMINATION_ACTION"
field is optional. If this field is omitted, the default termination action is stop."instanceTerminationAction": "TERMINATION_ACTION"
field is required.If you want the VM to start running, start the VM by using the instances.start
method:
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/start
Replace the following:
PROJECT_ID
: the project id of the project that contains the VM.ZONE
: the zone that contains the VM.VM_NAME
: the name of the VM.You can monitor the run time of a VM that has a time limit by using the following options:
To see when a running VM is scheduled to be automatically terminated, view the VM's termination timestamp:
terminationTimestamp
field.To verify if a VM was automatically terminated, view the VM's operations:
In the output, you can identify VM operations that are caused by a time limit by looking for the following types of operations:
compute.instances.deferredStop
indicates an automatic termination action of stopcompute.instances.deferredDelete
indicates an automatic termination action of deleteLearn how to further optimize your VMs:
Learn about more options for automatically limiting the run times of VMs:
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["This document details how to set time limits for virtual machine (VM) instances, enabling automatic termination (stop or delete) after a specified duration or at a set time."],["Setting time limits on VMs helps manage temporary workloads, minimize costs, and free up quotas by automatically terminating them upon reaching the defined limit."],["You can configure a new or existing VM to be automatically terminated either by specifying a maximum run duration (from 30 seconds to 120 days) or by setting a specific termination time, with the ability to choose whether the VM should be stopped or deleted."],["When setting a duration for a time limit, the termination timestamp is calculated by adding the duration to the VM's start time, whereas setting a specific time defines the exact termination point, but the specified time must always be in the future."],["To monitor VM run time, you can view the VM's `terminationTimestamp` field to see when it's scheduled for termination, or check the VM operations for `compute.instances.deferredStop` or `compute.instances.deferredDelete` to verify if the VM was automatically stopped or deleted due to a time limit."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4