# Volumes

URL: https://briq.cloud/docs/concepts/volumes

> Named volumes that outlive a briq. A briq starts from a copy of one, and can publish its own volume as the next copy.

A [volume](https://briq.cloud/docs/concepts/briq#disk) dies with its briq. A **named volume** is one the team keeps:
the next briq that names it starts with the data the last publishing briq left.

```text
briq_run({ image: "ghcr.io/acme/app:24.04", size: "large",
  volume: { mount: "/work", gb: 50, from: "deps-linux-x64", publish: true } })
→ bq_7kq2m9x1p4ab app (…) running · volume deps-linux-x64: restored, generation 12
```

1. `from` names a volume of your team. A name that does not exist yet starts an empty volume.
2. The briq gets its **own copy**. Nothing it writes reaches the volume or any other briq, so briqs
   in parallel never see each other's files.
3. With `publish: true`, the briq's volume becomes the volume's next copy when the briq is
   destroyed after a clean stop. A volume is made by its first publish; there is no create call.

A name is lowercase letters and digits, joined by single dots or dashes, up to 63 characters:
`deps-linux-x64`, `data.v2`.

## What a briq gets

The briq's `volume.from` says what it got:

```json
{ "volume": "vol_…", "name": "deps-linux-x64", "generation": 12, "restored": true }
```

A briq that starts empty has `"restored": false` and a `reason`. It runs with an empty volume of the
size asked. This is a normal start, not an error.

| `reason`           | Meaning                                                        | Publishes? |
| ------------------ | -------------------------------------------------------------- | ---------- |
| `new`              | The name has no copy yet                                       | Yes        |
| `expired`          | The copy was deleted before the briq started                   | Yes        |
| `lost`             | The machine that held the copy lost it                         | Yes        |
| `host_unavailable` | The machine that holds the copy is down, draining or upgrading | Yes        |
| `capacity`         | The machine that holds the copy had no room                    | No         |
| `busy`             | Too many briqs were starting from this volume there at once    | No         |
| `stack`            | The briq joined a stack that runs on another machine           | No         |
| `size`             | A larger copy was published while this briq was being admitted | No         |
| `unsupported`      | This environment has no named volumes                          | No         |

A briq that starts empty and does not publish leaves the volume where it is, so one busy moment
does not move it. A lost volume is made again by the next publishing briq.

## Publish

Every later briq that names it reads what a publishing briq wrote. Whoever sets `publish` decides
what later briqs read.

* A briq that starts from a name without `publish` only reads. What it writes goes away with it.
* Keep credentials off the volume: only data you want every later briq to read belongs on the mount.

A publish needs a clean stop. A destroy call stops the briq first, and the volume is published only
when that stop is clean. Otherwise the briq is destroyed without publishing.

* `briq_destroy` with `publish: false` skips the publish.
* `onExit: destroy` publishes only after exit code 0.
* A briq that went to sleep does not publish.

The last publish wins. Two briqs that finish together each publish, and the later one is the copy.

Once a briq that asked to publish is destroyed, its `volume.from.published` says what came of it:

```json
{ "generation": 13 }
{ "generation": null, "reason": "unclean" }
```

| `reason`   | Meaning                                                                                                        |
| ---------- | -------------------------------------------------------------------------------------------------------------- |
| `unclean`  | The briq did not stop cleanly, for example it was asleep                                                       |
| `refused`  | The machine refused or could not publish it                                                                    |
| `skipped`  | It started empty by placement, exited non-zero under `onExit: destroy`, or was destroyed with `publish: false` |
| `stale`    | A later publish already held the copy, or the briq started before the volume moved machines                    |
| `deleting` | The volume was being deleted                                                                                   |

## How long a volume lives

|              |                                                                                                              |
| ------------ | ------------------------------------------------------------------------------------------------------------ |
| Idle limit   | 7 days by default, 1 to 30 per volume (`idleDays`). A restore or a publish starts the count again            |
| Attached     | A volume is never deleted while a briq is attached to it                                                     |
| Delete       | `briq_volume_destroy`, or the dashboard. The volume is `deleting` at once: a briq that names it starts empty |
| Not a backup | A volume can be lost with its machine. A lost one starts the next briq empty, and nothing restores it        |

A volume never shrinks. A briq that asks for more `gb` than the volume has gets the larger volume,
and its publish keeps the larger size. A volume counts toward the team's
[disk](https://briq.cloud/docs/cost#plans) from its first publish until it is deleted or lost. A `deleting` volume
counts only while its copy is still on a host, and a lost one holds no disk. Every briq not
destroyed holds its own copy too, stopped and failed ones included. A volume deleted while a briq
that started from it is still running keeps counting until that briq is destroyed and the machine
confirms the delete.

## Limits

* A briq with `from` cannot be forked.
* `from` and `publish` are for an image run. A recipe mounts its own volume, and a recipe run that sends them is refused.
* A volume with `copy: "never"` takes neither `from` nor `publish`.
* A volume lives in one region. A briq placed in another region starts empty.

## Manage volumes

| Surface   | List                    | Delete                             |
| --------- | ----------------------- | ---------------------------------- |
| MCP       | `briq_volumes`          | `briq_volume_destroy`              |
| CLI       | `briq volumes`          | `briq volume-destroy <name or id>` |
| REST      | `GET /v1/volumes`       | `DELETE /v1/volumes/:id`           |
| SDK       | `client.volumes.list()` | `client.volumes.delete(id)`        |
| Python    | `client.volumes.list()` | `client.volumes.delete(id)`        |
| Dashboard | **Volumes**             | **Delete** in the row              |

A volume shows its name, size, state, generation, last use and expiry. `pending` is a name a briq that asked to
publish was created with, and that has no copy yet. A sandbox credential cannot list, name or delete volumes.
