Skip to main content
This page shows how to terminate sandboxes reliably so resources are released when your code finishes or exits unexpectedly. Use these patterns to avoid leaving orphaned sandboxes running after your script completes. Use a context manager to handle cleanup automatically:
with Sandbox.run() as sandbox:
    sandbox.exec(["echo", "hello"]).result()
# Stopped automatically
For sandboxes created without a context manager, call .stop() explicitly:
sandbox = Sandbox.run()
sandbox.exec(["echo", "hello"]).result()
sandbox.stop().result()
As a safety net, the SDK registers global cleanup handlers for atexit and signals (Ctrl+C, SIGTERM), so sandboxes stop even on unexpected exits. For batch cleanup, tagging strategies, and orphan recovery, see the Cleanup patterns guide.
Last modified on May 29, 2026