# Snapshot a volume

Copy a volume's blocks into a snapshot in its zone, incrementally, and delete snapshots you no longer need.

A volume snapshot is the volume's raw blocks, cut into 4 MiB pieces and stored in the zone. To
restore one, see [Restore a volume snapshot](restore-a-volume-snapshot.md). Replace the ids with
your own.

**Important:**
A snapshot of an attached volume stops its instance for the whole copy and starts it again
afterwards. Connections to it drop.

## Prerequisites

- Permission for `ec2:CreateSnapshot`; with an attached volume also `ec2:StopInstances` on its
  instance. Deleting needs `ec2:DeleteSnapshot`.
- In the console, also `ec2:DescribeVolumes` to open **Volumes** and `ec2:DescribeSnapshots` to
  open **Volume snapshots** (without either, its page says "Access denied").
- The volume is `available` or `in-use`. A volume still `creating` or in `error` is refused with
  `409` "The volume is not ready".
- No other snapshot or restore of the volume is running, else `409` "Snapshot … of this volume is
  in progress; try again when it is done" or "This volume is being restored from …; wait until it
  is available".
- Room in the volume snapshots quota.

## Take a snapshot

**Console**
1. Open **Compute**, then **Volumes**.
2. In the volume's row menu, choose **Create snapshot**.
3. (Optional) For **Name**, enter before-upgrade.
4. If the volume is attached, under **Attached instance**, select **Stop _instance_ for the
   snapshot and start it again afterwards**.
5. Choose **Create snapshot**. **Volume snapshots** opens.

**CLI**
```sh
dcloud ec2 snapshots create vol-1a2b3c4d --name before-upgrade --stop-instance
dcloud ec2 snapshots list --volume-id vol-1a2b3c4d
```

**API**
```http
POST /ec2/api/volumes/vol-1a2b3c4d/snapshots
{ "name": "before-upgrade", "stopInstance": true }
```

**Terraform**
```hcl
resource "dcloud_volume_snapshot" "before_upgrade" {
  volume_id     = dcloud_volume.data.id
  name          = "before-upgrade"
  stop_instance = true
}
```

An attached volume without `stopInstance` is refused with `409` "The volume is attached to ….
Detach it, or pass stopInstance: true to stop the instance for the snapshot and start it again
afterwards." With it, the instance is stopped, the volume detached and copied, then reattached and
the instance started again. A detached volume is copied without touching anything else.

## Result

The answer is `202` with a snapshot `snap-…` in state `pending`. It moves to `creating` (with
`progressPercent`) and then `completed`, or `error` with the reason. **Volume snapshots** shows the
progress. While a snapshot runs, the volume cannot be attached, detached, grown or deleted.

- **Encrypted volumes** are copied as they are on disk, still encrypted; the copy is made without
  the key. The snapshot keeps its own copy of the volume's key (wrapped like the volume's, under the
  same KMS key if you chose one), so it stays restorable after the volume is deleted.
- **Incremental.** A piece already stored by an earlier snapshot of any volume of your
  organization in that zone is not uploaded or stored again; all-zero pieces are not stored at all.
  `storedBytes` is the data the snapshot references; `newBytes` is what it was the first to store.
  Every block of the volume is read each time, however little changed.
- **Cost.** Snapshot storage is metered daily: each stored piece counts once, on the oldest
  snapshot that holds it. Where snapshot storage is not charged yet, it shows as "metered, not
  charged yet"; you are told before charging starts.

Measured on 2026-11-12, on a 50 GiB LUKS2-encrypted volume holding 4.3 GiB of files, snapshotted
with `stopInstance`: the first snapshot stored 5.43 GiB in 309 seconds end to end (stopping and
starting the instance included); a second one after 1 GiB of new data and one 128 MiB file
rewritten uploaded 1,200 MiB in 272 seconds.

## Delete a snapshot

**Console**
1. Open **Compute**, then **Volume snapshots**, and choose the snapshot in the list.
2. Under **Delete this snapshot**, choose **Delete snapshot**.
3. Enter the snapshot's name (its id if it has none) and choose **Delete snapshot**.

**CLI**
```sh
dcloud ec2 snapshots delete snap-5e6f7a8b
```

**API**
```http
DELETE /ec2/api/snapshots/snap-5e6f7a8b
```

**Terraform**
Remove the `dcloud_volume_snapshot` resource and run `terraform apply`.

The answer is `202` with the snapshot `deleting`; the pieces no other snapshot of your organization
uses are removed. A delete is refused with `409` "The snapshot is still being taken; delete it
after" while it is `pending` or `creating`, and with `409` "A volume is being restored from this
snapshot" during a restore.

## Related

- [Restore a volume snapshot](restore-a-volume-snapshot.md)
- `ec2.snapshots.create` · `dcloud ec2 snapshots delete` · `dcloud_volume_snapshot`
