Remove environment variables from your sandbox. You can delete individual variables or clear all custom variables at once.
Overview
Clearing environment variables is useful for:
- Removing sensitive data
- Resetting configuration
- Cleaning up temporary variables
- Preparing sandbox for reuse
Deleting individual variables preserves other variables. System variables (like PATH, HOME) are typically preserved and cannot be deleted.
Delete Single Variable
Delete a specific environment variable:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Set some variables
sandbox.env.set_all({
"API_KEY": "sk-123",
"DEBUG": "true",
"TEMP_TOKEN": "temp-xyz"
})
# Delete a single variable
sandbox.env.delete("TEMP_TOKEN")
print("TEMP_TOKEN deleted")
# Verify deletion
env = sandbox.env.get_all()
print(f"TEMP_TOKEN exists: {'TEMP_TOKEN' in env}")
print(f"API_KEY still exists: {'API_KEY' in env}")
sandbox.kill()
Delete Multiple Variables
Delete multiple variables one by one:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Set multiple variables
sandbox.env.set_all({
"API_KEY": "sk-123",
"DATABASE_URL": "postgres://localhost/db",
"DEBUG": "true",
"TEMP_TOKEN": "temp-xyz",
"OLD_CONFIG": "deprecated"
})
# Delete multiple variables
variables_to_delete = ["TEMP_TOKEN", "OLD_CONFIG", "DEBUG"]
for var in variables_to_delete:
sandbox.env.delete(var)
print(f"Deleted: {var}")
# Verify remaining variables
env = sandbox.env.get_all()
print(f"\nRemaining variables: {list(env.keys())}")
sandbox.kill()
Clear All Custom Variables
Clear all custom environment variables:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Set custom variables
sandbox.env.set_all({
"API_KEY": "sk-123",
"DATABASE_URL": "postgres://localhost/db",
"DEBUG": "true"
})
# Get current variables
before = sandbox.env.get_all()
print(f"Variables before: {len(before)}")
# Clear all custom variables (set empty dict)
sandbox.env.set_all({})
# Get variables after clearing
after = sandbox.env.get_all()
print(f"Variables after: {len(after)}")
# System variables may still exist
system_vars = ['PATH', 'HOME', 'USER', 'SHELL']
remaining = [v for v in after.keys() if v in system_vars]
print(f"System variables preserved: {remaining}")
sandbox.kill()
Clean Up Temporary Variables
Remove temporary or sensitive variables:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Set up environment with temporary variables
sandbox.env.set_all({
"API_KEY": "sk-prod-xyz",
"DATABASE_URL": "postgres://localhost/db",
"TEMP_TOKEN": "temp-abc123",
"SESSION_ID": "session-xyz",
"DEBUG": "true"
})
# Clean up temporary variables
temp_vars = ["TEMP_TOKEN", "SESSION_ID"]
for var in temp_vars:
sandbox.env.delete(var)
print(f"Cleaned up: {var}")
# Verify cleanup
env = sandbox.env.get_all()
print(f"\nRemaining variables: {list(env.keys())}")
# Production variables should still exist
print(f"API_KEY exists: {'API_KEY' in env}")
print(f"DATABASE_URL exists: {'DATABASE_URL' in env}")
sandbox.kill()
Complete Example
Hereโs a complete example showing environment variable cleanup:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Set up complete environment
print("๐ง Setting up environment...")
sandbox.env.set_all({
"API_KEY": "sk-prod-xyz",
"DATABASE_URL": "postgres://localhost/prod",
"REDIS_URL": "redis://localhost:6379",
"TEMP_TOKEN": "temp-abc123",
"SESSION_ID": "session-xyz",
"DEBUG": "true",
"LOG_LEVEL": "info"
})
env = sandbox.env.get_all()
print(f"โ
Environment set up with {len(env)} variables\n")
# Use environment in code
print("๐ป Using environment in code...")
result = sandbox.run_code('''
import os
print(f"API key: {os.getenv('API_KEY', 'not set')[:10]}...")
print(f"Database: {os.getenv('DATABASE_URL', 'not set')}")
print(f"Temp Token: {os.getenv('TEMP_TOKEN', 'not set')}")
''')
print(result.stdout)
# Clean up temporary variables
print("\n๐งน Cleaning up temporary variables...")
temp_vars = ["TEMP_TOKEN", "SESSION_ID"]
for var in temp_vars:
sandbox.env.delete(var)
print(f" โ
Deleted: {var}")
# Verify cleanup
print("\n๐ Final environment:")
final_env = sandbox.env.get_all()
for key in sorted(final_env.keys()):
if key not in ['PATH', 'HOME', 'USER', 'SHELL', 'PWD']:
if any(sensitive in key.upper() for sensitive in ['KEY', 'SECRET', 'PASSWORD']):
print(f" {key}=***MASKED***")
else:
print(f" {key}={final_env[key]}")
sandbox.kill()
Best Practices
1. Delete Individual Variables
Use delete(key) to remove specific variables while preserving others.
2. Clean Up Temporaries
Regularly delete temporary variables (tokens, session IDs) to reduce security risk.
3. Clear Before Reuse
Clear all custom variables before reusing a sandbox for a different purpose.
4. Verify Deletion
Always verify variables are deleted using get_all() or get() after deletion.
5. Preserve System Variables
System variables (PATH, HOME, etc.) are typically preserved and cannot be deleted.
Next Steps