# Sandbox life cycle
<subtitle>Manage the entire process of the sandbox from creation to closure, including running upper limit, timeout settings, suspension recovery and manual destruction. </subtitle>

The sandbox will keep running for the timeout you set. After the timeout expires, the sandbox is handled by the system; if auto-pause is enabled, the sandbox can enter a pause state to release computing resources while retaining a full running state, and can later be resumed for continued use. You can also set a timeout explicitly, or manually destroy the sandbox after the task is completed.

> UCloud Sandbox has a single continuous running limit of **24 hours** for all users. For workloads longer than 24 hours, use [Pause and Resume](/docs/agent-sandbox/sandbox/persistence.md): Pause resets the continuous run window and leaves the sandbox state intact.

## Create sandbox and set timeout

When starting the sandbox, the default timeout is 5 minutes (300 seconds). You can customize this parameter according to your needs.

> Note: When automatic suspension is not enabled, the timed-out sandbox will be automatically recycled and cleaned by the system. When you need to retain the running state, please refer to [Sandbox Persistence](/docs/agent-sandbox/sandbox/persistence.md).

```python
from ucloud_sandbox import Sandbox

# Create a sandbox and set the survival time to 60 seconds
# Parameter unit is seconds
sandbox = Sandbox.create(
    timeout=60,
)
```

## Dynamically adjust survival time

You can call the `set_timeout` method to update the survival time in real time while the sandbox is running.

Each time this method is called, the sandbox timeout is reset to the new value you specify and recalculated starting from the current point in time. This is useful for scenarios where the session needs to be extended dynamically based on user interaction. For example, you could create a sandbox with a 1-minute timeout and renew it on each user interaction.

```python
from ucloud_sandbox import Sandbox

#Create sandbox, initial 60 seconds
sandbox = Sandbox.create(timeout=60)

# Readjust to 30 seconds in subsequent business logic
# The sandbox will run for another 30 seconds from this point
sandbox.set_timeout(30)
```

## Get running details

The `get_info` method allows you to retrieve sandbox information, including the sandbox ID, template used, metadata, and precise start and end times.

```python
from ucloud_sandbox import Sandbox

sandbox = Sandbox.create(timeout=60)

# Get sandbox details
info = sandbox.get_info()

print(info)

# Output example:
# SandboxInfo(
#   sandbox_id='ig6f1yt6idvxkxl562scj-419ff533',
#   template_id='u7nqkmpn3jjf1tvftlsu',
#   name='base',
#   metadata={},
#   started_at=datetime.datetime(2025, 3, 24, 15, 42, 59, 255612),
#   end_at=datetime.datetime(2025, 3, 24, 15, 47, 59, 255612)
# )
```

## Manually destroy the sandbox

You can destroy the sandbox by calling the `kill` method at any time before the timeout expires. After the business process ends, it is recommended to proactively destroy sandboxes that are no longer used to release resources.

```python
from ucloud_sandbox import Sandbox

sandbox = Sandbox.create(timeout=60)

# Destroy and recycle the sandbox immediately
sandbox.kill()
```
