> ## Documentation Index
> Fetch the complete documentation index at: https://novita.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Sandbox Snapshot

A snapshot is a reusable saved state of a sandbox. It captures the sandbox filesystem and memory state so you can start new sandboxes from that point later.

Novita also describes this concept as committing a sandbox into a snapshot. Use snapshots when you want to:

* Prepare dependencies once and reuse them in later sandboxes.
* Save a known-good state before running risky or expensive work.
* Start multiple sandboxes from the same captured environment.
* Keep a reusable state after the original sandbox is no longer needed.

For continuing the same sandbox later, use [Sandbox Persistence](/guides/sandbox-persistence). For creating new sandboxes from a saved point, use snapshots.

## Terminology

* **Origin Sandbox**: The sandbox instance used to create the snapshot.
* **Snapshot**: The saved state that can be used to create new sandboxes.
* **New Sandbox**: A sandbox created from the snapshot.

## Create a snapshot

You can use the `createSnapshot` method in JavaScript or `create_snapshot` method in Python to create a snapshot from a running sandbox. The sandbox is paused while the snapshot is being created, then can continue running after the operation completes.

<CodeGroup>
  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox

  sandbox = Sandbox.create()
  sandbox.commands.run("echo ready > /tmp/status.txt")

  snapshot = sandbox.create_snapshot()
  print(snapshot.snapshot_id)
  ```

  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  const sandbox = await Sandbox.create()
  await sandbox.commands.run('echo ready > /tmp/status.txt')

  const snapshot = await sandbox.createSnapshot()
  console.log(snapshot.snapshotId)
  ```
</CodeGroup>

You can also create a snapshot by sandbox ID.

<CodeGroup>
  ```python Python icon="python" theme={"system"}
  snapshot = Sandbox.create_snapshot(sandbox_id)
  ```

  ```js JavaScript & TypeScript icon="js" theme={"system"}
  const snapshot = await Sandbox.createSnapshot(sandboxId)
  ```
</CodeGroup>

Store the returned snapshot ID. Then you can use it as the template when creating a new sandbox.

## Create a sandbox from a snapshot

You can pass the snapshot ID to `create` to start a new sandbox from the captured state.

<CodeGroup>
  ```python Python icon="python" theme={"system"}
  snapshot_id = snapshot.snapshot_id

  new_sandbox = Sandbox.create(template=snapshot_id)
  result = new_sandbox.commands.run("cat /tmp/status.txt")
  print(result.stdout)
  ```

  ```js JavaScript & TypeScript icon="js" theme={"system"}
  const snapshotId = snapshot.snapshotId

  const newSandbox = await Sandbox.create(snapshotId)
  const result = await newSandbox.commands.run('cat /tmp/status.txt')
  console.log(result.stdout)
  ```
</CodeGroup>

The new sandbox has its own sandbox ID and lifecycle. Killing the original sandbox does not delete the snapshot.

## List snapshots

You can use the `listSnapshots` method in JavaScript or `list_snapshots` method in Python when you need to find saved states for a sandbox or for your account.

<CodeGroup>
  ```python Python icon="python" theme={"system"}
  # List snapshots created from one sandbox.
  paginator = sandbox.list_snapshots(limit=20)

  while paginator.has_next:
      for snapshot in paginator.next_items():
          print(snapshot.snapshot_id)

  # Or list all snapshots.
  paginator = Sandbox.list_snapshots(limit=20)
  ```

  ```js JavaScript & TypeScript icon="js" theme={"system"}
  // List snapshots created from one sandbox.
  const paginator = sandbox.listSnapshots({ limit: 20 })

  while (paginator.hasNext) {
    const snapshots = await paginator.nextItems()
    for (const snapshot of snapshots) {
      console.log(snapshot.snapshotId)
    }
  }

  // Or list all snapshots.
  const allSnapshots = Sandbox.listSnapshots({ limit: 20 })
  ```
</CodeGroup>

You can filter by source sandbox ID when listing all snapshots.

<CodeGroup>
  ```python Python icon="python" theme={"system"}
  paginator = Sandbox.list_snapshots(sandbox_id=sandbox_id, limit=20)
  ```

  ```js JavaScript & TypeScript icon="js" theme={"system"}
  const paginator = Sandbox.listSnapshots({ sandboxId, limit: 20 })
  ```
</CodeGroup>

## Delete a snapshot

You can use the `deleteSnapshot` method in JavaScript or `delete_snapshot` method in Python to delete snapshots you no longer need. A snapshot may not be deletable while running sandboxes are still using it as their source, so kill those sandboxes before deleting the snapshot.

<CodeGroup>
  ```python Python icon="python" theme={"system"}
  new_sandbox.kill()
  Sandbox.delete_snapshot(snapshot_id)
  ```

  ```js JavaScript & TypeScript icon="js" theme={"system"}
  await newSandbox.kill()
  await Sandbox.deleteSnapshot(snapshotId)
  ```
</CodeGroup>

## Snapshot recommendations

* Snapshot after setup steps finish successfully.
* Keep snapshot IDs in your own database if jobs need to reuse them later.
* Use metadata on sandboxes to track where saved states came from.
* Delete unused snapshots to avoid keeping stale saved states.
* Use [Sandbox Persistence](/guides/sandbox-persistence) instead if you only need to pause and resume the same sandbox.
