Skip to content

Use snapshots

Last updated View as MarkdownAgent setup

Snapshots let you save point-in-time filesystem state from a running Container. The examples on this page use the Durable Object Container API.

Create a container snapshot

Use snapshotContainer() to capture the full container filesystem. Snapshots are immutable. If you restore a snapshot and then change files, create a new snapshot to persist those changes.

The returned snapshot handle is a plain data object. You can store it and restore it later, including from another Durable Object:

src/index.jsjs
import { DurableObject } from "cloudflare:workers";

export class MyDurableObject extends DurableObject {
	async saveContainer() {
		const containerSnapshot = await this.ctx.container.snapshotContainer({
			name: "before-upgrade",
		});

		await this.ctx.storage.put("containerSnapshot", containerSnapshot);
	}
}
src/index.tsts
import { DurableObject } from "cloudflare:workers";

export class MyDurableObject extends DurableObject {
	async saveContainer() {
		const containerSnapshot = await this.ctx.container.snapshotContainer({
			name: "before-upgrade",
		});

		await this.ctx.storage.put("containerSnapshot", containerSnapshot);
	}
}

Restore a container snapshot

Load the saved snapshot handle. Then, pass it to this.ctx.container.start() when you start another container:

src/index.jsjs
import { DurableObject } from "cloudflare:workers";

export class MyDurableObject extends DurableObject {
	async restoreContainer() {
		const containerSnapshot = await this.ctx.storage.get("containerSnapshot");

		if (!containerSnapshot) {
			return;
		}

		this.ctx.container.start({ containerSnapshot, enableInternet: false });
	}
}
src/index.tsts
import { DurableObject } from "cloudflare:workers";

export class MyDurableObject extends DurableObject {
	async restoreContainer() {
		const containerSnapshot =
			await this.ctx.storage.get<ContainerSnapshot>("containerSnapshot");

		if (!containerSnapshot) {
			return;
		}

		this.ctx.container.start({ containerSnapshot, enableInternet: false });
	}
}

Understand retention

Snapshots have an implicit 30-day time-to-live. Each restore refreshes that time-to-live.

You cannot set a custom time-to-live yet.

Was this helpful?