Limit the run time of a VM Stay organized with collections Save and categorize content based on your preferences.
This document explains how to limitthe run time of new or existing virtual machine (VM) instances and how tomonitor the run time of those VMs. For managed instance groups (MIGs), seeLimit the run time of a MIGinstead.
When limiting the run time of a VM, you can schedule the VM to be automaticallyterminated (stopped or deleted) when it reaches a specifictime limit(duration or time).Use time limits to help optimize temporary workloads—by automaticallylimiting VM run times, you can help minimize costs and free up quotas.
Note:GPU VMs that are configured to be automatically deletedafter a predefined run time of 7 days or less can consume either preemptible orstandard allocation quotas. This behavior is intendedto help you improve the obtainability of allocation quota fortemporary-but-uninterrupted workloads. For more information about thisbehavior, seeGPU VMs and preemptible allocation quotas.To learn how to immediately stop a VM, seeStop or restart a VM.To learn how to immediately delete a VM, seeDelete a VM.
Before you begin
To get the permissions that you need to limit the run time of a VM, ask your administrator to grant you theCompute Instance Admin (v1) (
roles/compute.instanceAdmin.v1) IAM role on the project. For more information about granting roles, seeManage access to projects, folders, and organizations.You might also be able to get the required permissions throughcustom roles or otherpredefined roles.
- If you haven't already, set upauthentication. 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:
gcloudinit
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.- Set a default region and zone.
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. After installation,initialize the Google Cloud CLI by running the following command:
gcloudinit
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.For more information, seeAuthenticate for using REST in the Google Cloud authentication documentation.
Restrictions
Limiting the run time of a VM has the following restrictions:
The automatic termination action must be either stop or delete. You cannotconfigure a VM to be automatically suspended when the time limit is reached.
The minimum time limit is 30 seconds and the maximumtime limit is 120 days.
Note: If you want to schedule a VM to stop after more than 120 days, youcan useinstance schedulesinstead.Automatic termination might take up to 30 seconds longer than the durationor time you specify to begin stopping or deleting the VM.
You cannot use time limits withlegacy preemptible VMs.Instead, use time limits withSpot VMs.
A VM's local SSD data cannot be preserved when the VM isautomatically stopped due to a time limit. For more information, seeStop a VM with Local SSD.
Limit the run time of a new VM
The following sections describe how to configure a time limitwhile creating a new VM. You can specify the time limit, which is when you wanta VM to automatically terminate, as either aduration(maxRunDuration)or atime (terminationTime).
When deciding the type of time limit for a VM, consider that the VM might beinterrupted—for example, by a user request or by ahost event.The behavior of a time limit varies based on the type of interruptionand the type of time limit:
A VM'stermination timestamp (
terminationTimestamp)is a read-only field that represents the planned time for automatictermination and is automatically defined byCompute Engine whenever a VM has a time limit and enters theRUNNINGstate.The termination timestamp is automatically cleared whenever a VM isstopped or suspended.However, a termination timestamp doesn't change when youreset or reboot the VM.
The termination timestamp is automatically redefined whenever the VM isrestarted or resumed based on the type of time limit that you set:
- If you set a duration for the VM, the termination timestamp isrecalculated by adding that duration to the VM's latest start time.
- If you set a time for the VM, the termination timestamp isset to that time. However, the time must be in the future; otherwise,any requests to create or rerun the VM fail until you update or removethe time.
Set a duration
Aduration represents the total run time that you want for a VM.To create a VM that is automatically terminated after the VM runs for aspecific duration, use the Google Cloud console, Google Cloud CLI, Terraform, orCompute Engine API.
Console
In the Google Cloud console, go to theCreate an instance page.
In theAvailability policies section, expandVM provisioning model advanced settings.
Select theSet a time limit for the VM checkbox. TheTime limit type field appears.
In theTime limit type field, selectBy hours (default)to specify the time limit as a duration.In the following field, enter the duration in hours.
In theOn VM termination list, select what happenswhen the run time of the VM reaches the specified time limit:
- To automatically stop the VM, selectStop (default).
- To delete the VM, selectDelete.
Optional: Specify other VM options. For more information,seeCreating and starting a VM instance.
To create and start the VM, clickCreate.
gcloud
To create a VM from the gcloud CLI, use thegcloud compute instances create command.
- To create a VM that is automatically terminated after a specific duration,you must include the
--max-run-durationflag. - To specify the termination action, include the
--instance-termination-actionflag. - Configure the
--discard-local-ssds-at-termination-timestampflag:- If the VM has any local SSDs and the termination action(
TERMINATION_ACTION) is stop (STOP), you mustinclude the--discard-local-ssds-at-termination-timestamp=trueflag. - Otherwise, omit the
--discard-local-ssds-at-termination-timestampflag.
- If the VM has any local SSDs and the termination action(
gcloud compute instances createVM_NAME \ --max-run-duration=DURATION \ --instance-termination-action=TERMINATION_ACTION
Replace the following:
VM_NAME: Thename of thenew VM.DURATION: The duration you want this VMto run before being automatically terminated. Format the durationas the number of days, hours, minutes, and secondsfollowed byd,h,m, andsrespectively. For example, specify30mfor a duration of 30 minutes, or specify1d2h3m4sfor aduration of 1 day, 2 hours, 3 minutes, and 4 seconds. The minimumduration is 30 seconds (30s) and the maximum duration is 120 days(120d).TERMINATION_ACTION: The termination action forthis VM, which can be either stop (STOP) or delete (DELETE).Whether this field is required or has a default value varies based onthe VM's provisioning model:- If this is a Spot VM (if the VM uses the
--provisioning-model=SPOTflag), the--instance-termination-action=TERMINATION_ACTIONflag is optional. If this flag is omitted, the default terminationaction is stop. - Otherwise (default), the
--instance-termination-action=TERMINATION_ACTIONflag isrequired.
- If this is a Spot VM (if the VM uses the
For more information about other options you can specify when creating a VM,seeCreating and starting a VM instance.
Terraform
To create a VM using Terraform, use thegoogle_compute_instance resource.
- To create a VM that is automatically terminated after a specific duration,you must include the
max_run_durationargument. - To specify the termination action, include the
instance_termination_actionargument.You must include theinstance_termination_actionargument unless you arecreating a Spot VM (set theprovisioning_modelargumenttoSPOT), which defaults to stop (STOP). - Configure the
on_instance_stop_actionargument:- If the VM has any local SSDs and the termination action(
instance_termination_actionargument) is stop (STOP), you mustset theon_instance_stop_actionargument to true (true). - Otherwise, omit the
on_instance_stop_actionargument.
- If the VM has any local SSDs and the termination action(
For more information, see theTerraform documentation for thegoogle_compute_instance resource.
REST
To create a VM from the Compute Engine API, use theinstances.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 themaxRunDuration field. To specify thetermination action, include theinstanceTerminationActionfield, 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: Theproject idof the project to create the VM in.ZONE: Thezoneto create the VM in. The zone must also support themachine type to use for the new VM.MACHINE_TYPE: Thepredefinedorcustommachine type for the new VM.VM_NAME: Thename of thenew VM.IMAGE_PROJECT: Theproject containing the image.For example, if you specifyfamily/debian-10as the image,specifydebian-cloudas the image project.IMAGE: The image for the new VM. You canspecify either a specific version of a publicimage or animage family.For example, if you specifyfamily/debian-10as the image anddebian-cloudas the image project, Compute Engine creates aVM from the latest version of the OS image in the Debian 10 image family.DURATION: The duration in seconds that you wantthis VM to run before being automatically terminated. The minimumduration is 30 seconds (30s) and the maximum duration is 120 days(120d).TERMINATION_ACTION: The termination action forthis VM, which can be either stop (STOP) or delete (DELETE).Whether this field is required or has a default value varies based onthe VM's provisioning model:- If this is a Spot VM (if the VM uses the
"provisioningModel": "SPOT"field), the"instanceTerminationAction": "TERMINATION_ACTION"field is optional. If this field is omitted, the default terminationaction is stop. - Otherwise (default), the
"instanceTerminationAction": "TERMINATION_ACTION"field isrequired.
- If this is a Spot VM (if the VM uses the
For more information about the options you can specify when creating a VM, seeCreating and starting a VM instance.
Set a time
Atime represents the date, time, and timezone when you want a VM tobe terminated. To create a VM that is automatically terminated at a specifictime, use the Google Cloud console, Google Cloud CLI, or Compute Engine API.
Console
In the Google Cloud console, go to theCreate an instance page.
In theAvailability policies section, expandVM provisioning model advanced settings.
Select theSet a time limit for the VM checkbox. TheTime limit type field appears.
In theTime limit type field, selectBy date to specifythe time limit as a time and date. In the following field, clickSelect date and time and select the date, time, and timezone forthe time limit.
In theOn VM termination list, select what happenswhen the run time of the VM reaches the specified time limit:
- To automatically stop the VM, selectStop (default).
- To delete the VM, selectDelete.
Optional: Specify other VM options. For more information,seeCreating and starting a VM instance.
To create and start the VM, clickCreate.
gcloud
To create a VM from the gcloud CLI, use thegcloud compute instances create command.
- To create a VM that is automatically terminated at a specific time,you must include the
--termination-timeflag. - To specify the termination action, include the
--instance-termination-actionflag, which is optional forSpot VMs. - Configure the
--discard-local-ssds-at-termination-timestampflag:- If the VM has any local SSDs and sets the termination action(
TERMINATION_ACTION) to stop (STOP), you mustinclude the--discard-local-ssds-at-termination-timestamp=trueflag. - Otherwise, omit the
--discard-local-ssds-at-termination-timestampflag.
- If the VM has any local SSDs and sets the termination action(
gcloud compute instances createVM_NAME \ --termination-time=TIME \ --instance-termination-action=TERMINATION_ACTION
Replace the following:
VM_NAME: Thename of thenew VM.TIME: The time you want this VMto be automatically terminated. The time you specify must be at least 30seconds in the future and at most 120 days in the future. Format the timeas aRFC 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 hourusing 24-hour time, 2-digit minute, and 2-digit secondseparated by colons.OFFSET: The time zone formatted as an offsetof Coordinated Universal Time (UTC). For example, to usePacific Standard Time (PST), which is 8 hours earlier than UTC, specify-08:00. Alternatively, to use no offset (UTC+0), specifyZ.
TERMINATION_ACTION: The termination action forthis VM, which can be either stop (STOP) or delete (DELETE).Whether this field is required or has a default value varies based onthe VM's provisioning model:- If this is a Spot VM (if the VM uses the
--provisioning-model=SPOTflag), the--instance-termination-action=TERMINATION_ACTIONflag is optional. If this flag is omitted, the default terminationaction is stop. - Otherwise (default), the
--instance-termination-action=TERMINATION_ACTIONflag isrequired.
- If this is a Spot VM (if the VM uses the
For more information about other options you can specify when creating a VM,seeCreating and starting a VM instance.
REST
To create a VM from the Compute Engine API, use theinstances.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 theterminationTime field. To specify thetermination action, include theinstanceTerminationActionfield, 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: Theproject idof the project to create the VM in.ZONE: Thezoneto create the VM in. The zone must also support themachine type to use for the new VM.MACHINE_TYPE: Thepredefinedorcustommachine type for the new VM.VM_NAME: Thename of thenew VM.IMAGE_PROJECT: Theproject containing the image.For example, if you specifyfamily/debian-10as the image,specifydebian-cloudas the image project.IMAGE: The image for the new VM. You canspecify either a specific version of a publicimage or animage family.For example, if you specifyfamily/debian-10as the image anddebian-cloudas the image project, Compute Engine creates aVM from the latest version of the OS image in the Debian 10 image family.TIME: The time you want this VMto be automatically terminated. The time you specify must be at least 30seconds in the future and at most 120 days in the future. Format the timeas aRFC 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 hourusing 24-hour time, 2-digit minute, and 2-digit secondseparated by colons.OFFSET: The time zone formatted as an offsetof Coordinated Universal Time (UTC). For example, to usePacific Standard Time (PST), which is 8 hours earlier than UTC, specify-08:00. Alternatively, to use no offset (UTC+0), specifyZ.
TERMINATION_ACTION: The termination action forthis VM, which can be either stop (STOP) or delete (DELETE).Whether this field is required or has a default value varies based onthe VM's provisioning model::- If this is a Spot VM (if the VM uses the
"provisioningModel": "SPOT"field), the"instanceTerminationAction": "TERMINATION_ACTION"field is optional. If this field is omitted, the default terminationaction is stop. - Otherwise (default), the
"instanceTerminationAction": "TERMINATION_ACTION"field isrequired.
- If this is a Spot VM (if the VM uses the
For more information about the options you can specify when creating a VM, seeCreating and starting a VM instance.
Limit the run time of an existing VM
You 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 fortime limits, review the previous sections about how tolimit the run time of a new VM first.
You can use the Google Cloud console, Google Cloud CLI, or Compute Engine API toupdate the VM's scheduling-related properties, as described in this section.This method requires that you first stop your VM, then update itsproperties, then restart it.Alternatively, if you want to simultaneously update additional VM properties andautomatically stop and restart a VM, seeUpdate instance propertiesinstead.
Console
In the Google Cloud console, go to theVM instances page.
In theName column, click the name of the VM that you wantto update.
From theVM instance details page, complete the following steps:
- If the VM is running, clickStop to stop the VM.
- To edit the VM, clickEdit.
On theEdit instance page, complete the following steps:
In theAvailability policies section, modify theSet a time limit for the VM checkboxand any fields below it as desired.
For more information about how to configure the properties fortime limits, seelimit the run time of a new VM.
To save your changes, clickSave.
Optional: If you want to start running the VM now, clickStart.
gcloud
To update a VM's time limit by using thegcloud CLI, complete the following steps:
If the VM is running, stop the VM by using the
gcloud compute instances stopcommand:gcloud compute instances stopVM_NAME
Replace
VM_NAMEwith the name of VM thatyou want to update.Update the VM's time limit by using the
gcloud compute instances set-schedulingcommand.gcloud compute instances set-schedulingVM_NAME \ --max-run-duration=DURATION \ --termination-time=TIME \ --instance-termination-action=TERMINATION_ACTION \ --discard-local-ssds-at-termination-timestamp=true
Configure the following flags:
- You must omit either the
--max-run-durationflag or the--termination-timeflag to set the time limitas either a time or duration, respectively. - Include
--instance-termination-actionflag to set thetermination action. - If the VM has any local SSDs and sets the termination action(
TERMINATION_ACTION) to stop (STOP), youmust include the--discard-local-ssds-at-termination-timestamp=trueflag.Otherwise, omit the--discard-local-ssds-at-termination-timestamp=trueflag.
Then, replace the following:
VM_NAME: Thename of theVM that you want to update.DURATION: The duration you want this VMto run before being automatically terminated. Format the durationas the number of days, hours, minutes, and secondsfollowed byd,h,m, andsrespectively. For example, specify30mfor a duration of 30 minutes, or specify1d2h3m4sfor aduration of 1 day, 2 hours, 3 minutes, and 4 seconds. The minimumduration is 30 seconds (30s) and the maximum duration is 120 days(120d).TIME: The time you want this VMto be automatically terminated. The time you specify must be at least 30seconds in the future and at most 120 days in the future. Format the timeas aRFC 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 hourusing 24-hour time, 2-digit minute, and 2-digit secondseparated by colons.OFFSET: the time zone formatted as an offsetof Coordinated Universal Time (UTC). For example, to usePacific Standard Time (PST), which is 8 hours earlier than UTC, specify-08:00. Alternatively, to use no offset (UTC+0), specifyZ.
TERMINATION_ACTION: The termination action forthis VM, which can be either stop (STOP) or delete (DELETE).Whether this field is required or has a default value varies based onthe VM's provisioning model:- If this is a Spot VM (if the VM uses the
--provisioning-model=SPOTflag), the--instance-termination-action=TERMINATION_ACTIONflag is optional. If this flag is omitted, the default terminationaction is stop. - Otherwise (default), the
--instance-termination-action=TERMINATION_ACTIONflag is required.
- If this is a Spot VM (if the VM uses the
- You must omit either the
If you want the VM to start running, start the VM by using the
gcloud compute instances startcommand:gcloud compute instances startVM_NAME
Replace
VM_NAMEwith the name of the VM.
REST
To update a VM's time limit by using theCompute Engine API, complete the following steps:
If the VM is running, stop the VM by using the
instances.stopmethod:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop
Replace the following:
PROJECT_ID: Theproject idof the project that contains the VM.ZONE: Thezonethat contains the VM.VM_NAME: Thename of theVM that you want to update.
Update the VM's
schedulingproperty, including fields forthe VM's time limit, by using theinstances.setSchedulingmethod.Include either themaxRunDurationfield or theterminationTimefieldto set the time limit as either a duration or time, respectively:To set aduration, 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 atime, 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: Theproject idof the project to create the VM in.ZONE: Thezoneto create the VM in. The zone must also support themachine type to use for the new VM.MACHINE_TYPE: Thepredefinedorcustommachine type for the new VM.VM_NAME: Thename of thenew VM.IMAGE_PROJECT: Theproject containing the image.For example, if you specifyfamily/debian-10as the image,specifydebian-cloudas the image project.IMAGE: The image for the new VM. You canspecify either a specific version of a publicimage or animage family.For example, if you specifyfamily/debian-10as the image anddebian-cloudas the image project, Compute Engine creates aVM from the latest version of the OS image in the Debian 10 image family.DURATION: The duration in seconds that you wantthis VM to run before being automatically terminated. The minimumduration is 30 seconds (30s) and the maximum duration is 120 days(120d).TIME: The time you want this VMto be automatically terminated. The time you specify must be at least 30seconds in the future and at most 120 days in the future. Format the timeas aRFC 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 hourusing 24-hour time, 2-digit minute, and 2-digit secondseparated by colons.OFFSET: The time zone formatted as an offsetof Coordinated Universal Time (UTC). For example, to usePacific Standard Time (PST), which is 8 hours earlier than UTC, specify-08:00. Alternatively, to use no offset (UTC+0), specifyZ.
TERMINATION_ACTION: The termination action forthis VM, which can be either stop (STOP) or delete (DELETE).Whether this field is required or has a default value varies based onthe VM's provisioning model:- If this is a Spot VM (if the VM uses the
"provisioningModel": "SPOT"field), the"instanceTerminationAction": "TERMINATION_ACTION"field is optional. If this field is omitted, the default terminationaction is stop. - Otherwise (default), the
"instanceTerminationAction": "TERMINATION_ACTION"field isrequired.
- If this is a Spot VM (if the VM uses the
If you want the VM to start running, start the VM by using the
instances.startmethod:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/start
Replace the following:
PROJECT_ID: theproject idof the project that contains the VM.ZONE: thezonethat contains the VM.VM_NAME: thename of theVM.
Monitor the run time of a VM
You can monitor the run time of a VM that has a time limit byusing the following options:
To see when a running VM is scheduled to be automatically terminated,view the VM's termination timestamp:
- View the details of a VM.
- In the output, view the VM's field for the termination timestamp:
- If you are using the Google Cloud console, see theMax duration field.
- If you are using the Google Cloud CLI or Compute Engine API, see the
terminationTimestampfield.
To verify if a VM was automatically terminated, view the VM's operations:
- View VM operations.
In the output, you can identify VM operations that are caused bya time limit by looking for the following types of operations:
compute.instances.deferredStopindicates an automatic termination action of stopcompute.instances.deferredDeleteindicates an automatic termination action of delete
What's next
Learn how to further optimize your VMs:
- If your workloads are fault-tolerant, you can useSpot VMsto get significant discounts and optional dedicated quota.
- If you want a VM to automatically execute commands before shutting down,you can useshutdown scripts.
Learn about more options for automatically limiting the run times of VMs:
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.