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

# Save, restore, fork

> Save checkpoints, restore previous work, and create copies for parallel tasks.

Save a snapshot when you want to return to a sandbox's current state. It includes
files, memory, and running processes. Use a fork when you want another working
copy immediately.

## Save and restore

Create a sandbox, write a file, and save a durable checkpoint:

```python theme={null}
from lithosbox import LithosBox

box = LithosBox()
sandbox = box.sandboxes.create()
sandbox.files.write("/work/result.txt", "First result\n")
snapshot = sandbox.snapshot(wait_durable=True)
print(snapshot.id)
```

The original sandbox keeps running. `wait_durable=True` waits until the snapshot
has finished saving and can be used if the original sandbox becomes unavailable.

Restore that snapshot into a new sandbox:

```python theme={null}
restored = box.snapshots.restore(snapshot.id)
print(restored.id)
print(restored.files.read("/work/result.txt"))
```

The restored sandbox has its own ID and uses the snapshot's CPU and memory
configuration. You cannot change that configuration during restore. Changes made
after the snapshot are not included, and changes to the restored copy do not
change the original.

## Check an existing snapshot

You can create a snapshot without waiting for durability, then check it later:

```python theme={null}
checkpoint = sandbox.snapshot()
checkpoint = box.snapshots.wait_durable(checkpoint.id)
print(checkpoint.durable)
```

A snapshot may be restorable before it is durable. Wait for durability before
deleting the original sandbox or relying on the checkpoint for recovery. If a
wait times out, check the snapshot again; the wait ending does not cancel saving.

Find snapshots in **Console → Snapshots** or with `box.snapshots.list()`.

To save a checkpoint and let the original sandbox rest immediately, use
`snapshot(leave_paused=True, wait_durable=True)`. Its next command or file access
wakes it automatically; it does not require `resume()`.

## Fork for parallel work

Create an independent copy of the current sandbox:

```python theme={null}
child = sandbox.fork()
child.run("printf 'Alternative result\n' > /work/result.txt").check()
print(sandbox.files.read("/work/result.txt"))
child.delete()
```

The original still contains `First result`. To create several copies from the same
point, request them together:

```python theme={null}
children = sandbox.fork(n=4)
try:
    for child in children:
        print(child.id)
finally:
    for child in children:
        child.delete()
```

`fork()` returns one sandbox. Passing `n` returns a list, including when `n=1`.
You can create up to four copies per call, within your organization's sandbox limit.

Fork between commands whenever possible. Copies inherit running processes and
connection state, so reconnect external services separately in each copy before
using them. Avoid copying a process that is partway through an external write.

## Clean up

Snapshots remain after you delete their original sandbox. Delete each resource
when you no longer need it:

```python theme={null}
restored.delete()
sandbox.delete()
box.snapshots.delete(snapshot.id)
box.snapshots.delete(checkpoint.id)
box.close()
```

To download a snapshot's files outside LithosBox, see
[Snapshots and exports](/reference/http-api#snapshots-and-exports).
