[ Web Proxy ]
URL:
Viewing: https://cloud.google.com/compute/docs/instances/limit-vm-runtime [Back]  [Original]

Limit the run time of a VM  |  Compute Engine  |  Google Cloud Documentation Skip to main content
Google Cloud Documentation [Google Cloud Documentation]
Send feedback

Limit the run time of a VM 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 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 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:

Set a duration

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.

Console

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

    Go to Create an instance

  2. In the Availability policies section, expand VM provisioning model advanced settings.

  3. Select the Set a time limit for the VM checkbox. The Time limit type field appears.

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

  5. In the On VM termination list, select what happens when the run time of the VM reaches the specified time limit:

    • To automatically stop the VM, select Stop (default).
    • To delete the VM, select Delete.
  6. Optional: Specify other VM options. For more information, see Creating and starting a VM instance.

  7. To create and start the VM, click Create.

gcloud

To create a VM from the gcloud CLI, use the gcloud compute instances create command.

gcloud compute instances create VM_NAME \
    --max-run-duration=DURATION \
    --instance-termination-action=TERMINATION_ACTION

Replace the following:

For more information about other options you can specify when creating a VM, see Creating and starting a VM instance.

Terraform

To create a VM using Terraform, use the google_compute_instance resource.

For more information, see the Terraform documentation for the google_compute_instance resource.

REST

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:

For more information about the options you can specify when creating a VM, see Creating and starting a VM instance.

Set a time

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

Console

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

    Go to Create an instance

  2. In the Availability policies section, expand VM provisioning model advanced settings.

  3. Select the Set a time limit for the VM checkbox. The Time limit type field appears.

  4. In the Time limit type field, select By date to specify the time limit as a time and date. In the following field, click Select date and time and select the date, time, and timezone for the time limit.

  5. In the On VM termination list, select what happens when the run time of the VM reaches the specified time limit:

    • To automatically stop the VM, select Stop (default).
    • To delete the VM, select Delete.
  6. Optional: Specify other VM options. For more information, see Creating and starting a VM instance.

  7. To create and start the VM, click Create.

gcloud

To create a VM from the gcloud CLI, use the gcloud compute instances create command.

gcloud compute instances create VM_NAME \
    --termination-time=TIME \
    --instance-termination-action=TERMINATION_ACTION

Replace the following:

For more information about other options you can specify when creating a VM, see Creating and starting a VM instance.

REST

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

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

Console

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

    Go to VM instances

  2. In the Name column, click the name of the VM that you want to update.

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

    1. If the VM is running, click Stop to stop the VM.
    2. To edit the VM, click Edit.
    3. On the Edit instance page, complete the following steps:

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

      2. To save your changes, click Save.

    4. Optional: If you want to start running the VM now, click Start.

gcloud

To update a VM's time limit by using the gcloud CLI, complete the following steps:

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

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

    • You must omit either the --max-run-duration flag or the --termination-time flag to set the time limit as either a time or duration, respectively.
    • Include --instance-termination-action flag to set the termination action.
    • If the VM has any local SSDs and sets 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:

      • If this is a Spot VM (if the VM uses the --provisioning-model=SPOT flag), the --instance-termination-action=TERMINATION_ACTION flag is optional. If this flag is omitted, the default termination action is stop.
      • Otherwise (default), the --instance-termination-action=TERMINATION_ACTION flag is required.
  3. 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.

REST

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

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

      • 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 termination action is stop.
      • Otherwise (default), the "instanceTerminationAction": "TERMINATION_ACTION" field is required.
  3. 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.

Monitor the run time of a VM

You can monitor the run time of a VM that has a time limit by using the following options:

What's next

Send feedback

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 2026-08-12 UTC.

Need to tell us more? [[["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 2026-08-12 UTC."],[],[]]

Web Proxy Viewer  |  New URL  |  Original Page