Kill a Sandbox

Updated at:

Killing a Sandbox releases its resources. Explicitly kill the Sandbox when a task finishes, encounters an unrecoverable error, or exceeds the time your application is willing to wait.

Kill the Current Sandbox

await sandbox.kill();

Python example:

sandbox.kill()

It is recommended to release resources in finally:

const sandbox = await Sandbox.create("code-interpreter-v1", {
  apiKey: process.env.E2B_API_KEY,
  apiUrl: process.env.E2B_API_URL,
  domain: process.env.E2B_DOMAIN,
});

try {
  await sandbox.commands.run("npm test");
} finally {
  await sandbox.kill();
}

Python example:

sandbox = Sandbox.create(
    template="code-interpreter-v1",
    api_key=os.environ["E2B_API_KEY"],
    api_url=os.environ["E2B_API_URL"],
    domain=os.environ["E2B_DOMAIN"],
)

try:
    sandbox.commands.run("python3 -m pytest")
finally:
    sandbox.kill()

Difference from pause

kill() ends the Sandbox lifecycle, while pause() suspends the Sandbox so it can be resumed later. After a Sandbox is killed, you cannot return to the same runtime state with Sandbox.connect().

If the task only needs to wait briefly for the user to continue, use Pause and Resume. If the task is complete, kill the Sandbox.