# Auto-stop

> Stop machines automatically a fixed time after they start, so forgotten machines don't keep billing.

Auto-stop stops a machine a set time after it starts running. New machines stop after **1 hour** unless you choose another duration or turn auto-stop off. The deadline counts from when the machine becomes running; activity inside the machine does not extend it. To stop a machine once it has done nothing for a while instead, turn on [idle sleep](/docs/concepts/idle-sleep); with both on, whichever comes first stops it.

## Settings

| Field | Meaning |
| --- | --- |
| `auto_stop_after_seconds` | 60 to 2,592,000 seconds (1 minute to 30 days). `null` means never. Creates default to 3600. |
| `never_auto_stop` | `true` turns auto-stop off. It can't be combined with `auto_stop_after_seconds`. |
| `auto_stop_at` | The current deadline (UTC, RFC 3339), present only while the machine is meant to run with a deadline. |

## Set it when you create a machine

```sh tab="CLI"
vy new --name dev --auto-stop 2        # hours; also 30m, 1d, or never
```

```ts tab="TypeScript"
await vy.machines.create({ name: "dev", autoStopAfterSeconds: 2 * 3600 });
await vy.machines.create({ name: "always-on", neverAutoStop: true });
```

```python tab="Python"
vy.machines.create(name="dev", auto_stop_after_seconds=2 * 3600)
vy.machines.create(name="always-on", never_auto_stop=True)
```

```sh tab="curl"
# In the create body:
#   "auto_stop_after_seconds": 7200      or      "never_auto_stop": true
```

## Change it later

`PATCH /v1/machines/{id}` changes the setting and returns the updated machine right away. Changing auto-stop on a running machine restarts its deadline from now; renaming alone leaves the deadline as it is.

```sh tab="CLI"
vy autostop dev 30m
vy autostop dev never
```

```ts tab="TypeScript"
await machine.update({ autoStopAfterSeconds: 30 * 60 });
await machine.update({ neverAutoStop: true });
```

```python tab="Python"
machine.update(auto_stop_after_seconds=30 * 60)
machine.update(auto_stop_after_seconds=None)  # never
```

```sh tab="curl"
curl -X PATCH "$VMYARD_API_URL/v1/machines/$MACHINE_ID" \
  -H "Authorization: Bearer $VMYARD_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"auto_stop_after_seconds": 1800}'
```

## How the stop happens

VMYard checks deadlines every second. When one passes, it requests an ordinary stop operation for that machine, exactly once per deadline, even if you change the setting or stop the machine at the same moment. The stop emits `machine.stopped` like any other stop, and its operation has `stop_reason: "auto_stop"`.

A start or restart sets a new deadline. A stop or delete clears it.
