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

# Cleanup

> Terminate sandboxes and manage cleanup with context managers, explicit stops, and global atexit handlers.

이 예시에서는 `CWSANDBOX_API_KEY`에 설정된 CoreWeave API 액세스 토큰을 사용합니다. Forge API 키 인증은 [시작하기](/ko/products/sandboxes/get-started)를 참조하세요. CKS 요구 사항은 [배치 모드](/ko/products/sandboxes/placement)를 참조하세요.

이 페이지에서는 코드 실행이 끝나거나 예기치 않게 종료될 때 리소스가 해제되도록 샌드박스를 안정적으로 종료하는 방법을 설명합니다. 스크립트가 완료된 후에도 고아 샌드박스가 계속 실행되는 것을 방지하려면 다음 패턴을 사용하세요.

컨텍스트 관리자를 사용하여 정리를 자동으로 처리하세요:

```python theme={"system"}
with Sandbox.run() as sandbox:
    sandbox.exec(["echo", "hello"]).result()
# 자동으로 중단
```

컨텍스트 관리자 없이 생성한 샌드박스는 `.stop()`을 명시적으로 호출하세요:

```python theme={"system"}
sandbox = Sandbox.run()
sandbox.exec(["echo", "hello"]).result()
sandbox.stop().result()
```

안전 장치로, SDK는 `atexit`과 signal(Ctrl+C, `SIGTERM`)에 대한 전역 cleanup handler를 등록합니다. 이 handler는 [`Session`](/ko/products/sandboxes/client/guides/sessions)에 속한 샌드박스를 중지하므로, 세션 관리 샌드박스는 예기치 않은 종료 시에도 중지됩니다. `Sandbox.run()`으로 직접 생성한 샌드박스는 세션의 일부가 아니므로, process가 종료된 후에도 사용자가 중지하거나 lifetime이 만료될 때까지 계속 실행됩니다. 이 워크플로에 의존하는 예시는 [Sandboxes SDK로 Claude Code 설정하기](/ko/products/sandboxes/agents/claude-code#set-up-with-the-sandbox-sdk)를 참조하세요.

batch cleanup, tagging 전략, orphan recovery에 대해서는 [Cleanup patterns guide](/ko/products/sandboxes/client/guides/cleanup-patterns)를 참조하세요.
