Limit the run time of a VM

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

Restrictions

Limiting the run time of a VM has the following restrictions:

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 theRUNNING state.

  • 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

  1. In the Google Cloud console, go to theCreate an instance page.

    Go to Create an instance

  2. In theAvailability policies section, expandVM provisioning model advanced settings.

  3. Select theSet a time limit for the VM checkbox. TheTime limit type field appears.

  4. 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.

  5. 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.
  6. Optional: Specify other VM options. For more information,seeCreating and starting a VM instance.

  7. 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-duration flag.
  • To specify the termination action, include the--instance-termination-action flag.
  • Configure the--discard-local-ssds-at-termination-timestamp flag:
    • 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.
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, ands respectively. For example, specify30m for a duration of 30 minutes, or specify1d2h3m4s for 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=SPOT flag), 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.

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 themax_run_duration argument.
  • To specify the termination action, include theinstance_termination_action argument.You must include theinstance_termination_action argument unless you arecreating a Spot VM (set theprovisioning_model argumenttoSPOT), which defaults to stop (STOP).
  • Configure theon_instance_stop_action argument:
    • If the VM has any local SSDs and the termination action(instance_termination_action argument) is stop (STOP), you mustset theon_instance_stop_action argument to true (true).
    • Otherwise, omit theon_instance_stop_action argument.

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-10 as the image,specifydebian-cloud as 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-10 as the image anddebian-cloud as 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.

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

  1. In the Google Cloud console, go to theCreate an instance page.

    Go to Create an instance

  2. In theAvailability policies section, expandVM provisioning model advanced settings.

  3. Select theSet a time limit for the VM checkbox. TheTime limit type field appears.

  4. 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.

  5. 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.
  6. Optional: Specify other VM options. For more information,seeCreating and starting a VM instance.

  7. 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-time flag.
  • To specify the termination action, include the--instance-termination-action flag, which is optional forSpot VMs.
  • Configure the--discard-local-ssds-at-termination-timestamp flag:
    • 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.
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=SPOT flag), 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_ACTION flag isrequired.

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-10 as the image,specifydebian-cloud as 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-10 as the image anddebian-cloud as 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.

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

  1. In the Google Cloud console, go to theVM instances page.

    Go to VM instances

  2. In theName column, click the name of the VM that you wantto update.

  3. From theVM instance details page, complete the following steps:

    1. If the VM is running, clickStop to stop the VM.
    2. To edit the VM, clickEdit.
    3. On theEdit instance page, complete the following steps:

      1. 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.

      2. To save your changes, clickSave.

    4. 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:

  1. If the VM is running, stop the VM by using thegcloud compute instances stop command:

    gcloud compute instances stopVM_NAME

    ReplaceVM_NAME with the name of VM thatyou want to update.

  2. Update the VM's time limit by using thegcloud compute instances set-scheduling command.

    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-time flag to set the time limitas either a time or duration, respectively.
    • Include--instance-termination-action flag 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=true flag.Otherwise, omit the--discard-local-ssds-at-termination-timestamp=true flag.

    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, ands respectively. For example, specify30m for a duration of 30 minutes, or specify1d2h3m4s for 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=SPOT flag), 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.
  3. If you want the VM to start running, start the VM by using thegcloud compute instances start command:

    gcloud compute instances startVM_NAME

    ReplaceVM_NAME with the name of the VM.

REST

To update a VM's time limit by using theCompute Engine API, complete the following steps:

  1. If the VM is running, stop the VM by using theinstances.stop method:

    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.
  2. Update the VM'sscheduling property, including fields forthe VM's time limit, by using theinstances.setScheduling method.Include either themaxRunDuration field or theterminationTime fieldto 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-10 as the image,specifydebian-cloud as 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-10 as the image anddebian-cloud as 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.
  3. If you want the VM to start running, start the VM by using theinstances.start method:

    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:

    1. View the details of a VM.
    2. 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 theterminationTimestamp field.
  • To verify if a VM was automatically terminated, view the VM's operations:

    1. View VM operations.
    2. In the output, you can identify VM operations that are caused bya time limit by looking for the following types of operations:

      • compute.instances.deferredStop indicates an automatic termination action of stop
      • compute.instances.deferredDelete indicates an automatic termination action of delete

What's next

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.