Sandbox SDK Migration: Exec, Files, and Durable Object Control
How Sandbox SDK 1.0 replaces 0.x exec and file APIs, plans a one-way move, and when to use RPC transport on 0.x.
This page summarises Cloudflare documentation on moving from Sandbox SDK 0.x (especially 0.12) to Sandbox SDK 1.0 (@cloudflare/sandbox@next). In 0.x, the package’s Sandbox class is your Durable Object and talks to an SDK server inside the container. In 1.0, you implement the Durable Object, drive the sandbox through this.ctx.container, and use small helpers such as Files from @cloudflare/sandbox. Command and file behaviour changes are the usual migration breakpoints; Migrate from Sandbox SDK 0.x lists every feature page and an API map.
Sandbox SDK 1.0: what changes for exec and files
Section titled “Sandbox SDK 1.0: what changes for exec and files”Sandbox SDK 1.0 puts lifetime, previews, backups, and failure handling in your Durable Object class instead of the package Sandbox class.
In Sandbox SDK 0.x, the Sandbox class from the package is your Durable Object. It starts the container, keeps it running, and sends each call to a server inside the container. In 1.0, you write the Durable Object class, and it calls the container directly through this.ctx.container.
— Cloudflare, Changes in Sandbox SDK 1.0
this.ctx.container runs commands, connects to ports, intercepts outbound traffic, and saves snapshots. The npm package keeps helpers like Files; it does not write Durable Object storage, retry failed calls, or impose command timeouts—you implement those choices. Internet access is off unless start() passes enableInternet: true; per-host credentials belong in outbound handlers your class registers (Changes in Sandbox SDK 1.0).
How long can you stay on Sandbox SDK 0.x
Section titled “How long can you stay on Sandbox SDK 0.x”0.x apps keep running; @cloudflare/sandbox 0.x stays on npm with bug and security fixes until 2026-12-31 (Migrate from Sandbox SDK 0.x). Migration guides compare 1.0 with 0.12.14; older apps should upgrade to @cloudflare/sandbox@0.12.14 and docker.io/cloudflare/sandbox:0.12.14 first. Until you migrate, pin those versions (Migrate from Sandbox SDK 0.x).
Plan the move: one deploy or side by side
Section titled “Plan the move: one deploy or side by side”Pick a path before you touch production. You cannot undo the deploy that sets scheduling_policy: "durable_object"—after that, 0.x code cannot start containers for that class, even after wrangler rollback (Plan the move to Sandbox SDK 1.0).
| Path | What happens | Fits |
|---|---|---|
| In place | One deploy moves every sandbox; running 0.12 containers end; files in old containers are not carried over | Short-lived or easy-to-recreate sandboxes |
| Side by side | 1.0 class runs next to 0.12; each sandbox moves on first request | Long-lived sandboxes users return to |
Rehearse on a staging Worker with a different Worker name and container application name (Plan the move to Sandbox SDK 1.0). At the switch, running commands stop, WebSockets stop delivering data without a close frame, and new 1.0 containers start empty unless you use Move sandboxes side by side or backups (Plan the move to Sandbox SDK 1.0).
Replace getSandbox() with your own Durable Object class
Section titled “Replace getSandbox() with your own Durable Object class”Replace getSandbox() with env.NS.getByName(id) and methods on your class (Sandbox SDK 0.x API map). Each 0.x API maps to a native container call, a Files call, or code you write—see Replace the Sandbox class.
Discover which migrate pages you need:
grep -rhow \ -e getSandbox -e sleepAfter -e keepAlive \ -e envVars -e onStart -e onStop \ -e onActivityExpired -e createSession \ -e readFile -e writeFile -e listFiles -e watch \ -e startProcess -e streamProcessLogs \ -e exposePort -e proxyToSandbox -e wsConnect \ -e tunnels -e terminal -e SandboxAddon \ -e outboundByHost -e setOutboundHandler \ -e allowedHosts -e deniedHosts -e gitCheckout \ -e enableInternet -e interceptHttps \ -e mountBucket -e createBackup -e restoreBackup \ -e runCode -e createCodeContext \ -e sandbox/opencode -e sandbox/openai \ -e sandbox/bridge -e WarmPool \ src | sort | uniq -cThe Migrate from Sandbox SDK 0.x table links each symbol to a page (commands, files, preview URLs, tunnels, and others).
Image and sandbox-shim requirements
Section titled “Image and sandbox-shim requirements”0.12 images bundled tools such as Python, Git, and curl; images used in 1.0 migration docs may include none of them—you install what your commands need (Changes in Sandbox SDK 1.0). Files, S3Mount, and DirectoryBackup need the sandbox-shim helper copied into your image per package requirements (referenced from Change file calls).
exec() migration: no shell, no shared session
Section titled “exec() migration: no shell, no shared session”In 0.12, exec() ran each command string in a long-lived bash session under /workspace. In 1.0, container.exec() starts one process per call; nothing carries over between calls (Change command calls).
Replace 0.12 command calls with exec() on the container. In Sandbox SDK 0.12, exec() runs each command string in a long-lived bash session that starts in /workspace. In 1.0, each call starts one process, and nothing carries over from one call to the next.
— Cloudflare, Change command calls
How to call container.exec() instead of sandbox.exec()
Section titled “How to call container.exec() instead of sandbox.exec()”Pass an argument array, and set cwd and env explicitly—exec() starts in /, does not inherit start() env except PATH, and ignores image WORKDIR (Change command calls):
const process = await this.container.exec(["npm", "test"], { cwd: "/workspace", env: ENV,});const result = await process.output();exec() has no shell. For pipes, redirects, or shell variables, wrap with bash -c (Change command calls). A missing executable throws (0.12 bash returned exit code 127). Non-zero exit codes still do not throw; check result.exitCode.
exec() resolves when the process starts—await exitCode or output() before depending on side effects (Change command calls).
Replace createSession() with cwd and env objects
Section titled “Replace createSession() with cwd and env objects”Sessions are gone. Keep { cwd, env } in an object and pass it to every exec() (Change command calls). Steps that relied on cd or export across calls must run in one command, e.g. ["bash", "-c", "cd app && npm ci && npm test"]. For isolation between old sessions, use separate sandbox names (Change command calls).
Replace command timeout options with GNU timeout
Section titled “Replace command timeout options with GNU timeout”Use timeout from GNU coreutils in the argv list. Exit codes 124 or 137 usually mean the time limit fired; unlike 0.12, the process stops (Change command calls). Cancel from Worker code with AbortSignal on exec() (Change command calls).
Call ensureRunning() at the start of each method that runs commands (Change command calls).
Files migration: Files class and stricter path rules
Section titled “Files migration: Files class and stricter path rules”In 0.12 the in-container SDK server handled files (auto mkdir, base64 binaries, hidden dotfiles). In 1.0, Files runs sandbox-shim per call and does only what each method requests (Change file calls).
import { Files, SandboxFileError } from "@cloudflare/sandbox";
export class MySandbox extends DurableObject<Env> { private readonly files = new Files(this.container);}One Files instance per Durable Object lifetime is enough because this.ctx.container is stable (Change file calls). Call ensureRunning() before file operations.
Map readFile, writeFile, and listFiles to Files methods
Section titled “Map readFile, writeFile, and listFiles to Files methods”| 0.12 | 1.0 |
|---|---|
readFile(path) text → .content |
(await this.files.readFile(path)).text() |
readFileStream / stream helpers |
(await this.files.readFile(path)).body |
exists(path) |
files.stat(path); catch SandboxFileError code ENOENT |
mkdir, deleteFile, rename/move |
mkdir, remove, rename |
listFiles |
readDirectory + stat for sizes |
Relative paths need cwd in options—otherwise TypeError: cwd is required when path is relative (Change file calls).
writeFile and binary read differences
Section titled “writeFile and binary read differences”Create parent directories with mkdir({ recursive: true }) before writeFile; without that you get SandboxFileError ENOENT (Change file calls). Pass bytes, not base64 encoding: "base64". writeFile writes directly (readers may see partial files); for atomic replace, write a temp path and rename() (Change file calls).
readDirectory lists every entry including dotfiles; filter or walk as needed. stat().size is bigint—use Number() before JSON (Change file calls).
SandboxFileError and rename/remove semantics
Section titled “SandboxFileError and rename/remove semantics”Check errors with SandboxFileError.is(cause) and Linux-style code values (ENOENT, EEXIST, etc.) (Change file calls). remove() can delete directories with recursive: true. rename() follows Linux rename rules—not mv into an existing directory; cross-device moves need cp -a then remove() (Change file calls).
watch() and checkChanges() have no direct replacement; run inotifywait via container.exec() and stream output (Change file calls).
RPC transport and subrequest limits while still on 0.x
Section titled “RPC transport and subrequest limits while still on 0.x”If you remain on 0.x but run many SDK calls per Worker request, HTTP transport counts one Workers subrequest per operation (exec(), readFile(), etc.) (Limits).
Enable RPC transport to multiplex all SDK calls over a single persistent connection
— Cloudflare, Limits
Set Worker var SANDBOX_TRANSPORT to rpc (Environment variables, Transport modes):
{ "vars": { "SANDBOX_TRANSPORT": "rpc" }}RPC uses one persistent connection (one subrequest for the upgrade), multiplexes later operations, and removes HTTP’s 32 MiB limit for streaming writeFile() (Transport modes). WebSocket transport is deprecated; RPC is the recommended default (Deprecating Sandbox SDK features). On 1.0, SANDBOX_TRANSPORT is not needed—container.exec() and Files talk to the container directly (Sandbox SDK 0.x API map).
Deprecated 0.x features to avoid in new work
Section titled “Deprecated 0.x features to avoid in new work”Cloudflare deprecates HTTP/WebSocket transports, default persistent exec() sessions, exposePort() in favour of newer patterns, and removed APIs such as built-in desktop and WarmPool (Deprecating Sandbox SDK features). New projects should prefer @cloudflare/sandbox@next and the 1.0 preview rather than extending deprecated 0.x APIs.
Default sessions made cd persist across exec() calls, which confused agents; Cloudflare recommends enableDefaultSession: false on 0.x and createSession() only when you need that behaviour—both disappear in upcoming releases (Deprecating Sandbox SDK features). That aligns with 1.0’s explicit cwd/env per call.
Common migration mistakes
Section titled “Common migration mistakes”- Assuming
/workspaceandenvVars: 1.0exec()defaults to/and onlyPATHfromstart()—passcwd: "/workspace"andenv: ENVon every call (Change command calls). - Chaining exec without awaiting exit: Wait for
exitCode/output()before the next step (Change command calls). - writeFile without mkdir: 0.12 created parents automatically; 1.0 requires
mkdirfirst (Change file calls). - Relative file paths without
cwd: Always passcwdfor relative paths (Change file calls). - Skipping staging: Multi-version deploys cannot mix 0.x and 1.0 container applications in one gradual deployment (Plan the move to Sandbox SDK 1.0).
- Leaving the old container application: After migration, delete the legacy application from
wrangler containers listor old containers keep billing (Plan the move to Sandbox SDK 1.0).
Background processes, preview URLs, tunnels, terminals, backups, and runCode() each have dedicated migrate pages—use the grep table in Migrate from Sandbox SDK 0.x rather than guessing replacements.
How to check this on your site
Section titled “How to check this on your site”-
On a staging Worker, run the grep inventory from Migrate from Sandbox SDK 0.x and confirm every hit has a matching migrate page implemented.
-
Run the paired
container.exec()check from Change command calls (pwdandNODE_ENVwith and withoutcwd/env) and confirm output matches 0.12 expectations. -
After deploy, wait a few minutes, then list container applications with Wrangler (
npx wrangler containers list) and confirm the new 1.0 application is present. -
Delete the old container application ID once no 0.12 workloads remain, following Plan the move to Sandbox SDK 1.0.
-
You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact sentence you are asking about, and we will fix or clarify it.