Skip to main content
This example demonstrates how to manage sandbox lifecycle automatically using auto-stop, auto-archive, and auto-delete intervals.

What You’ll Learn

  • Setting auto-archive intervals
  • Configuring auto-delete policies
  • Understanding lifecycle states
  • Creating sandboxes with custom intervals
  • Updating intervals on existing sandboxes
  • Best practices for cost optimization

Auto-Archive Examples

Auto-archive automatically archives inactive sandboxes to reduce storage costs while preserving their state.
from daytona import CreateSandboxFromSnapshotParams, Daytona


def main():
    daytona = Daytona()

    # Default interval
    sandbox1 = daytona.create()
    print(sandbox1.auto_archive_interval)

    # Set interval to 1 hour
    sandbox1.set_auto_archive_interval(60)
    print(sandbox1.auto_archive_interval)

    # Max interval (never archive)
    sandbox2 = daytona.create(params=CreateSandboxFromSnapshotParams(auto_archive_interval=0))
    print(sandbox2.auto_archive_interval)

    # 1 day interval
    sandbox3 = daytona.create(params=CreateSandboxFromSnapshotParams(auto_archive_interval=1440))
    print(sandbox3.auto_archive_interval)

    sandbox1.delete()
    sandbox2.delete()
    sandbox3.delete()


if __name__ == "__main__":
    main()

Auto-Delete Examples

Auto-delete automatically removes sandboxes after they’ve been stopped for a specified period.
from daytona import CreateSandboxFromSnapshotParams, Daytona


def main():
    daytona = Daytona()

    # Auto-delete is disabled by default
    sandbox1 = daytona.create()
    print(sandbox1.auto_delete_interval)

    # Auto-delete after the Sandbox has been stopped for 1 hour
    sandbox1.set_auto_delete_interval(60)
    print(sandbox1.auto_delete_interval)

    # Delete immediately upon stopping
    sandbox1.set_auto_delete_interval(0)
    print(sandbox1.auto_delete_interval)

    # Disable auto-delete
    sandbox1.set_auto_delete_interval(-1)
    print(sandbox1.auto_delete_interval)

    # Auto-delete after the Sandbox has been stopped for 1 day
    sandbox2 = daytona.create(params=CreateSandboxFromSnapshotParams(auto_delete_interval=1440))
    print(sandbox2.auto_delete_interval)


if __name__ == "__main__":
    main()

Expected Output

Auto-Archive

30  # Default interval (30 minutes)
60  # Updated to 1 hour
0   # Never archive
1440  # 1 day (24 hours)

Auto-Delete

-1  # Disabled by default
60  # 1 hour after stop
0   # Immediate deletion
-1  # Disabled again
1440  # 1 day after stop

Key Concepts

Auto-Archive Intervals

Auto-archive helps reduce storage costs by archiving inactive sandboxes:
  • Default: 30 minutes of inactivity
  • 0: Never archive (sandbox stays active)
  • Positive number: Minutes of inactivity before archiving
  • Effect: Archived sandboxes consume minimal storage
# Create with custom interval
sandbox = daytona.create(
    params=CreateSandboxFromSnapshotParams(
        auto_archive_interval=120  # 2 hours
    )
)

# Update existing sandbox
sandbox.set_auto_archive_interval(60)  # 1 hour

Auto-Delete Intervals

Auto-delete permanently removes sandboxes after being stopped:
  • Default: -1 (disabled)
  • -1: Disabled (sandbox never auto-deletes)
  • 0: Delete immediately when stopped
  • Positive number: Minutes after stop before deletion
  • Effect: Sandbox and all data are permanently removed
Permanent deletion: Auto-delete permanently removes the sandbox and all its data. Use with caution!
# Disable auto-delete (default)
sandbox.set_auto_delete_interval(-1)

# Delete immediately on stop
sandbox.set_auto_delete_interval(0)

# Delete after 24 hours of being stopped
sandbox.set_auto_delete_interval(1440)

Lifecycle States

Sandboxes transition through these states:
running -> stopped -> archived
   ↓         ↓          ↓
deleted <- deleted <- deleted
  • running: Sandbox is active and consuming compute resources
  • stopped: Sandbox is paused, only storage costs apply
  • archived: Sandbox is compressed, minimal storage costs
  • deleted: Sandbox is permanently removed

Common Configurations

Development Sandboxes

# Short-lived dev environment
sandbox = daytona.create(
    params=CreateSandboxFromSnapshotParams(
        auto_archive_interval=15,  # Archive after 15 min
        auto_delete_interval=60    # Delete after 1 hour stopped
    )
)

Production Sandboxes

# Long-lived production environment
sandbox = daytona.create(
    params=CreateSandboxFromSnapshotParams(
        auto_archive_interval=0,   # Never archive
        auto_delete_interval=-1    # Never auto-delete
    )
)

Demo/Testing Sandboxes

# Temporary demo environment
sandbox = daytona.create(
    params=CreateSandboxFromSnapshotParams(
        auto_archive_interval=5,   # Quick archive (5 min)
        auto_delete_interval=0     # Delete immediately on stop
    )
)

CI/CD Sandboxes

# Ephemeral CI environment
sandbox = daytona.create(
    params=CreateSandboxFromSnapshotParams(
        auto_archive_interval=0,   # Don't archive
        auto_delete_interval=0     # Delete immediately when done
    )
)

Cost Optimization

Understanding Costs

StateCompute CostStorage CostRecovery Time
RunningFullFullImmediate
StoppedNoneFull~10 seconds
ArchivedNoneMinimal~30 seconds
DeletedNoneNoneCannot recover

Optimization Strategies

Development workflows: Set auto_archive_interval=30 to quickly archive idle dev sandboxes while keeping them recoverable.
CI/CD pipelines: Use auto_delete_interval=0 to immediately clean up test environments after job completion.
Long-running services: Set auto_archive_interval=0 and auto_delete_interval=-1 to prevent disruption.

Best Practices

1. Match Intervals to Use Case

# Short-lived: aggressive cleanup
test_sandbox = daytona.create(
    params=CreateSandboxFromSnapshotParams(
        auto_archive_interval=5,
        auto_delete_interval=30
    )
)

# Long-lived: conservative settings
production_sandbox = daytona.create(
    params=CreateSandboxFromSnapshotParams(
        auto_archive_interval=0,
        auto_delete_interval=-1
    )
)

2. Update Intervals Dynamically

# Start with aggressive cleanup
sandbox = daytona.create(
    params=CreateSandboxFromSnapshotParams(
        auto_delete_interval=60
    )
)

# If promoted to production, disable auto-delete
if is_production:
    sandbox.set_auto_delete_interval(-1)

3. Monitor Sandbox State

# Check before modifying
sandbox = daytona.get(sandbox_id)
if sandbox.state == "running":
    # Safe to extend lifecycle
    sandbox.set_auto_archive_interval(0)

4. Label for Tracking

sandbox.set_labels({
    "environment": "development",
    "auto_cleanup": "enabled",
    "retention": "1_day"
})

Interval Recommendations

By Use Case

Use CaseAuto-ArchiveAuto-DeleteRationale
Active Development30-60 min24 hoursBalance quick recovery and cleanup
CI/CD Pipeline0 (disabled)0 (immediate)Clean up immediately after use
Demo Environment5-10 min1-2 hoursQuick cleanup, short retention
Production0 (disabled)-1 (disabled)Always available, manual cleanup
Testing/QA15-30 min4-8 hoursModerate cleanup
Training/Workshops60 min7 daysAvailable during session, cleanup after

By Team Size

  • Individual developers: 30 min archive, 24 hour delete
  • Small teams: 60 min archive, 48 hour delete
  • Large organizations: Custom per project, use labels

Troubleshooting

Sandbox Deleted Unexpectedly

# Check auto-delete interval
sandbox = daytona.get(sandbox_id)
if sandbox.auto_delete_interval == 0:
    print("Warning: Sandbox will delete immediately on stop!")

Archive Taking Too Long

# Increase archive interval for large sandboxes
if sandbox.disk_size_gb > 10:
    sandbox.set_auto_archive_interval(120)  # 2 hours

Disable All Auto-Cleanup

# Completely disable automatic lifecycle management
sandbox.set_auto_archive_interval(0)   # Never archive
sandbox.set_auto_delete_interval(-1)   # Never delete

Next Steps

Basic Sandbox

Learn sandbox fundamentals

File Operations

Work with files in sandboxes

Build docs developers (and LLMs) love