Skip to content

Snapshots & forks

A snapshot saves a sandbox, memory and disk, as a template of its own. Sandboxes from it start where the original was: a repository cloned, its dependencies installed, a server listening. A fork snapshots a running sandbox and starts copies of it at once, while the original keeps running. Each copy changes on its own from then on.

MyTemplatetemplateWarmsnapshotuser-42sandboxfork 1sandboxfork 2sandboxnpm ciedit filestest shard 1test shard 2MyTemplatetemplateWarmsnapshotuser-42sandboxfork 1sandboxfork 2sandboxnpm ciedit filestest shard 1test shard 2
Warm is MyTemplate after npm ci. user-42 starts from Warm, and two forks copy user-42 as it is, edits included, to run test shards side by side.
alchemy.run.ts
const warm = yield* E2B.Snapshot("Warm", {
template: MyTemplate,
run: [
"git clone https://github.com/my-org/app /home/user/app",
"cd /home/user/app && npm ci",
],
});

Alchemy boots the template, runs the commands, snapshots the sandbox with its memory, and kills it. A command that exits non-zero fails the deploy with SnapshotSetupFailed. A deploy runs the setup again only when the template or the commands change.

const sandboxes = yield* E2B.CreateSandbox(Warm);
const sandbox = yield* sandboxes.create({ timeout: 600 });
yield* sandbox.exec("cd app && npm test");
const snapshot = yield* sandbox.snapshot({ name: `session-${id}` });
const restored = yield* sandboxes.create({ templateID: snapshot.snapshotID });

Pass memory: false to keep only the filesystem; sandboxes from it boot fresh.

const results = yield* sandbox.fork({ count: 3 });
for (const result of results) {
if (result._tag === "Forked") yield* result.sandbox.exec("npm test -- --shard");
}

E2B checkpoints the sandbox (it keeps running) and starts copies of it. Each fork succeeds or fails on its own.

const snapshots = yield* E2B.CreateSnapshot();
const info = yield* snapshots.create(`warm-${slug}`, { template: "node-22", run: ["npm ci"] });
yield* snapshots.delete(`warm-${slug}`);

Runtime snapshots are not deleted when you destroy the Worker.