E2B sandboxes are persistent — pause and resume preserves filesystem and memory state. Use snapshots to checkpoint and fork sandbox state.
E2B sandboxes are persistent environments. When you pause a sandbox, its full state — filesystem and memory, including all running processes and loaded variables — is preserved indefinitely. Resume it at any time and pick up exactly where you left off. Snapshots let you capture that state and spawn multiple new sandboxes from it.
When you pause a sandbox, both the sandbox’s filesystem and memory state will be saved. This includes all the files in the sandbox’s filesystem and all the running processes, loaded variables, data, etc.
Copy
Ask AI
import { Sandbox } from '@e2b/code-interpreter'const sbx = await Sandbox.create()console.log('Sandbox created', sbx.sandboxId)// Pause the sandbox// You can save the sandbox ID in your database to resume the sandbox laterawait sbx.pause()console.log('Sandbox paused', sbx.sandboxId)
When you resume a sandbox, it will be in the same state it was in when you paused it.
This means that all the files in the sandbox’s filesystem will be restored and all the running processes, loaded variables, data, etc. will be restored.
Copy
Ask AI
import { Sandbox } from '@e2b/code-interpreter'const sbx = await Sandbox.create()console.log('Sandbox created', sbx.sandboxId)// Pause the sandbox// You can save the sandbox ID in your database to resume the sandbox laterawait sbx.pause()console.log('Sandbox paused', sbx.sandboxId)// Connect to the sandbox (it will automatically resume the sandbox, if paused)const sameSbx = await sbx.connect()console.log('Connected to the sandbox', sameSbx.sandboxId)
You can list all paused sandboxes by calling the Sandbox.list method and supplying the state query parameter.
Copy
Ask AI
import { Sandbox, SandboxInfo } from '@e2b/code-interpreter'// List all paused sandboxesconst paginator = Sandbox.list({ query: { state: ['paused'] } })// Get the first page of paused sandboxesconst sandboxes = await paginator.nextItems()// Get all paused sandboxeswhile (paginator.hasNext) { const items = await paginator.nextItems() sandboxes.push(...items)}
You can remove paused sandboxes by calling the kill method on the Sandbox instance.
Copy
Ask AI
import { Sandbox } from '@e2b/code-interpreter'const sbx = await Sandbox.create()console.log('Sandbox created', sbx.sandboxId)// Pause the sandbox// You can save the sandbox ID in your database to resume the sandbox laterawait sbx.pause()// Remove the sandboxawait sbx.kill()// Remove sandbox by idawait Sandbox.kill(sbx.sandboxId)
When you connect to a sandbox, the inactivity timeout resets. The default is 5 minutes, but you can pass a custom timeout to the Sandbox.connect() method:
If you have a service (for example a server) running inside your sandbox and you pause the sandbox, the service won’t be accessible from the outside and all the clients will be disconnected.
If you resume the sandbox, the service will be accessible again but you need to connect clients again.
Auto-pause and auto-resume are configured together through the lifecycle object when creating a sandbox. Auto-pause suspends a sandbox when its timeout expires. Auto-resume wakes it back up when activity arrives.
Auto-pause is persistent — if your sandbox resumes and later times out again, it will pause again.If you call .kill(), the sandbox is permanently deleted and cannot be resumed.
Auto-resume is triggered by the sandbox activity - that’s both HTTP traffic and controlling the sandbox from the SDK.That includes SDK operations like:
sandbox.commands.run(...)
sandbox.files.read(...)
sandbox.files.write(...)
opening a tunneled app URL or sending requests to a service running inside the sandbox
If a sandbox is paused and autoResume is enabled, the next supported operation resumes it automatically. You do not need to call Sandbox.connect() first.
Use onTimeout: "pause" + autoResume: true so inbound traffic can wake a paused sandbox automatically.
This works for both basic web/API servers and dev or preview servers you open occasionally.
For queued tasks or tool calls, create once and keep using the same sandbox handle. If it is paused, it will auto-resume when you run the next command.
Copy
Ask AI
import { Sandbox } from 'e2b'// One-time setupconst sandbox = await Sandbox.create({ timeoutMs: 5 * 60 * 1000, lifecycle: { onTimeout: 'pause', autoResume: true, },})// Later: called for each agent/tool taskasync function runToolTask(command) { const result = await sandbox.commands.run(command) return result.stdout}console.log(await runToolTask('python -c "print(2 + 2)"'))
For multi-tenant apps, keep a map of sandbox IDs by user. On each request, connect to the user’s existing sandbox (which auto-resumes if paused) or create a new one.
Copy
Ask AI
import { Sandbox } from 'e2b'const userSandboxes = new Map() // userId → Sandboxasync function getSandbox(userId) { let sandbox = userSandboxes.get(userId) if (!sandbox) { sandbox = await Sandbox.create({ timeoutMs: 5 * 60 * 1000, lifecycle: { onTimeout: 'pause', autoResume: true, }, }) userSandboxes.set(userId, sandbox) } return sandbox}// On each user request (auto-resumes if paused)const sandbox = await getSandbox('user-123')const result = await sandbox.commands.run('echo "Hello from your sandbox"')console.log(result.stdout)
Auto-resume is persistent, meaning if your sandbox resumes and later times out again, it will pause again.If you call .kill(), the sandbox is permanently deleted and cannot be resumed.
Snapshots let you create a persistent point-in-time capture of a running sandbox, including both its filesystem and memory state.
You can then use a snapshot to spawn new sandboxes that start from the exact same state.The original sandbox continues running after the snapshot is created, and a single snapshot can be used to create many new sandboxes.
Snapshots require templates with envd version v0.5.0 or above. If you are using a custom template created before envd v0.5.0, you need to rebuild it.You can check the template envd version using the e2b template list command or by viewing the templates list on the dashboard.
The sandbox is briefly paused during the snapshot process but automatically returns to running state. The sandbox ID stays the same after the snapshot completes.
During the snapshot, the sandbox is temporarily paused and resumed. This causes all active connections (e.g. WebSocket, PTY, command streams) to be dropped. Make sure your client handles reconnection properly.
The snapshot ID can be used directly with Sandbox.create() to spawn a new sandbox from the snapshot. The new sandbox starts with the exact filesystem and memory state captured in the snapshot.
Copy
Ask AI
import { Sandbox } from 'e2b'const snapshot = await sandbox.createSnapshot()// Create a new sandbox from the snapshotconst newSandbox = await Sandbox.create(snapshot.snapshotId)
import { Sandbox } from 'e2b'// Returns true if deleted, false if the snapshot was not foundconst deleted = await Sandbox.deleteSnapshot(snapshot.snapshotId)
Both snapshots and templates create reusable starting points for sandboxes, but they solve different problems.
Templates
Snapshots
Defined by
Declarative code (Template builder)
Capturing a running sandbox
Reproducibility
Same definition produces the same sandbox every time
Captures whatever state exists at that moment
Best for
Repeatable base environments
Checkpointing, rollback, forking runtime state
Use templates when every sandbox should start from an identical, known state — pre-installed tools, fixed configurations, consistent environments.
Use snapshots when you need to capture or fork live runtime state that depends on what happened during execution.
Checkpointing agent work — an AI agent has loaded data and produced partial results in memory. Snapshot it so you can resume or fork from that point later.
Rollback points — snapshot before a risky or expensive operation (running untrusted code, applying a migration, refactoring a web app). If it fails, rollback - spawn a fresh sandbox from the snapshot before the operation happened.
Forking workflows — spawn multiple sandboxes from the same snapshot to explore different approaches in parallel.
Cached sandboxes — avoid repeating expensive setup by snapshotting a sandbox that has already loaded a large dataset or started a long-running process.
Sharing state — one user or agent configures an environment interactively, snapshots it, and others start from that exact state.