The sandbox persistence allows you to pause your sandbox and resume it later from the same state it was in when you paused it.
This includes not only state of the sandbox’s filesystem but also the sandbox’s memory. This means all running processes, loaded variables, data, etc.
The sandbox persistence feature is currently in Beta testing. You need to install the Beta version SDK to use it.
Please note:
It takes about 4 seconds per 1 GB RAM to pause the sandbox.
It takes about 1 second to resume the sandbox.
Sandbox can be paused up to 30 days.
After 30 days, the data will be deleted and you will not be able to resume the sandbox. Trying to resume sandbox that was deleted or does not exist will result in the NotFoundError error in JavaScript SDK and NotFoundException exception in Python SDK.
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'// or import { Sandbox } from 'e2b'// or import { Sandbox } from '@e2b/desktop'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 laterconst sandboxId = await sbx.pause()console.log('Sandbox paused', 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'// or import { Sandbox } from 'e2b'// or import { Sandbox } from '@e2b/desktop'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 laterconst sandboxId = await sbx.pause()console.log('Sandbox paused', sandboxId)// Resume the sandbox from the same stateconst sameSbx = await Sandbox.resume(sandboxId)console.log('Sandbox resumed', sameSbx.sandboxId)
You can list all paused sandboxes by calling the Sandbox.list method and supplying the state query parameter.
More information about using the method can be found in List Sandboxes.
Copy
Ask AI
import { Sandbox, SandboxInfo } from '@e2b/code-interpreter'// or import { Sandbox, SandboxInfo } from 'e2b'// or import { Sandbox, SandboxInfo } from '@e2b/desktop'// List all paused sandboxesconst paginator = Sandbox.list({ query: { state: ['paused'] } })// Get the first page of paused sandboxesconst sandboxes = await paginator.nextItems()// Get all paused sandboxesconst sandboxes: SandboxInfo[] = []while (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'// or import { Sandbox } from 'e2b'// or import { Sandbox } from '@e2b/desktop'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 laterconst sandboxId = await sbx.pause()// Remove the sandboxawait sbx.kill()// Remove sandbox by idawait Sandbox.kill(sandboxId)
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.