Skip to content

Volumes

A volume is a persistent disk, backed by object storage, that sandboxes mount at a path. Its files outlive every sandbox that mounts it, and several sandboxes can mount it at once. Daytona mounts volumes only when it creates a sandbox, so a sandbox’s mounts are fixed for its life.

volume user-sam/home/daytona/work keeps its filessam's sandboxessandbox, Mondaysandbox, Fridayvolume user-sam/home/daytona/work keeps its filessam's sandboxessandbox, Mondaysandbox, Friday
Sam's Monday sandbox is deleted, and Friday's new sandbox mounts the same volume at the same path. The files Sam left are still there.
src/Storage.ts
import * as Daytona from "@alchemy.run/daytona";
export const Cache = Daytona.Volume("Cache", {});
src/DevBox.ts
import * as Daytona from "@alchemy.run/daytona";
import { Base } from "./Images.ts";
import { Cache } from "./Storage.ts";
export const DevBox = Daytona.Sandbox("DevBox", {
snapshot: Base,
mounts: [{ volume: Cache, path: "/cache" }],
});
const cache = yield* Daytona.MountVolume(Cache, { path: "/cache" });
const sandbox = yield* sandboxes.create({ mounts: [cache] });

In a Worker, MountVolume returns a mount entry for the sandboxes you create. In a sandbox that runs a program, it mounts the volume into that sandbox.

const volumes = yield* Daytona.CreateVolume({ prefix: "user-" });
const home = yield* volumes.getOrCreate({ name: `user-${user}` });

The Worker’s key can read, create and delete volumes. getOrCreate waits until a new volume is ready. Volumes have only a name, so prefix is how you find the ones the Worker created: the binding refuses names outside it. They are not deleted when the stack is destroyed; volumes.list() and delete the names with your prefix to clean them up.

yield* volumes.delete(home.volumeId);

Deleting a volume deletes its files. A volume can’t be deleted while a sandbox mounts it, and Daytona releases it about a minute after that sandbox is deleted, so delete fails with VolumeInUse until then: retry later rather than holding a request open.

A volume is backed by object storage, so writing a whole file works but appending to an existing one doesn’t. Write a new file instead of >> onto an old one, like a file per visit or per run.

When you destroy the stack, a declared Daytona.Volume waits for the sandboxes that mount it to go first.