> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.modbox.run/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.modbox.run/_mcp/server.

# Destroy a sandbox

POST /sandboxes/destroy
Content-Type: application/json

Deletes every resource backing the sandbox (deployment, service, route, network policy and pull secret).

This operation is idempotent: destroying an already-destroyed sandbox succeeds. Sandboxes created with `ttl_seconds` are destroyed automatically when it elapses.

Reference: https://docs.modbox.run/api-reference/modbox-api/sandboxes/destroy-sandbox

## Authentication

- `Authorization` header (bearer token, required) — Bearer authentication of the form `Bearer <token>`, where token is your auth token.

## Request

### Headers

- `x-workspace-id` (string, optional, nullable) — UUID of the workspace that scopes this request. List the workspaces you belong to with GET /workspaces.

### Body (application/json)

This endpoint expects a DestroyRequest.

- `task_id` (string, required) — The name used when the sandbox was provisioned.

## Response

### 200

Successful Response

- `success` (boolean, required) — True when every resource was removed.
- `error` (string, optional, nullable) — Failure reason when `success` is false.

## Errors

### 401 Unauthorized Error

Unauthenticated — the Bearer token is missing or invalid.

- `error` (ErrorBody, required) — Machine-readable description of a failure.
- `detail` (string, optional, nullable) — DEPRECATED — mirrors 'error.message' for backwards compatibility. Read 'error.code' instead.

### 403 Forbidden Error

Forbidden — the caller lacks permission for this resource.

- `error` (ErrorBody, required) — Machine-readable description of a failure.
- `detail` (string, optional, nullable) — DEPRECATED — mirrors 'error.message' for backwards compatibility. Read 'error.code' instead.

### 404 Not Found Error

Not found — no such resource in this workspace.

- `error` (ErrorBody, required) — Machine-readable description of a failure.
- `detail` (string, optional, nullable) — DEPRECATED — mirrors 'error.message' for backwards compatibility. Read 'error.code' instead.

### 422 Unprocessable Entity Error

Validation error — see 'error.details' for the offending fields.

- `error` (ErrorBody, required) — Machine-readable description of a failure.
- `detail` (string, optional, nullable) — DEPRECATED — mirrors 'error.message' for backwards compatibility. Read 'error.code' instead.

## Types

### ErrorBody

Machine-readable description of a failure.

- `code` (enum, required) — Stable identifier for this class of failure. Branch on this value instead of parsing 'message'.
  - Allowed values: `unauthenticated`, `invalid_token`, `invalid_credentials`, `email_not_verified`, `forbidden`, `insufficient_role`, `workspace_required`, `workspace_forbidden`, `workspace_not_found`, `member_not_found`, `sandbox_not_found`, `sandbox_name_conflict`, `sandbox_not_ready`, `provisioning_failed`, `image_not_found`, `image_disabled`, `image_immutable`, `registry_not_found`, `api_key_not_found`, `validation_error`, `not_found`, `conflict`, `rate_limited`, `internal_error`, `upstream_error`
- `message` (string, required) — Human-readable explanation of what went wrong.
- `hint` (string, optional, nullable) — Concrete next step that is likely to resolve the error.
- `docs_url` (string, optional, nullable) — Link to the documentation section for this error code.
- `retryable` (boolean, optional, default: false) — True when retrying the identical request may succeed (transient failure). False means the request must change first.
- `request_id` (string, optional, nullable) — Correlation id, also returned in the 'X-Request-Id' header.
- `details` (list of ErrorDetail, optional, nullable) — Field-level problems, present on validation errors.

### ErrorDetail

A single field-level problem, typically from request validation.

- `issue` (string, required) — Human-readable description of what is wrong with this field.
- `field` (string, optional, nullable) — Dotted path to the offending field, e.g. 'body.task_id'.

## Examples

**Request**

```json
{
  "task_id": "agent-session-42"
}
```

**Response**

```json
{
  "success": true,
  "error": "string"
}
```

**SDK Code**

```python
import requests

url = "https://api.example.com/sandboxes/destroy"

payload = { "task_id": "agent-session-42" }
headers = {
    "Authorization": "Bearer <token>",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```javascript
const url = 'https://api.example.com/sandboxes/destroy';
const options = {
  method: 'POST',
  headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
  body: '{"task_id":"agent-session-42"}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://api.example.com/sandboxes/destroy"

	payload := strings.NewReader("{\n  \"task_id\": \"agent-session-42\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Authorization", "Bearer <token>")
	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby
require 'uri'
require 'net/http'

url = URI("https://api.example.com/sandboxes/destroy")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"task_id\": \"agent-session-42\"\n}"

response = http.request(request)
puts response.read_body
```

```java
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.example.com/sandboxes/destroy")
  .header("Authorization", "Bearer <token>")
  .header("Content-Type", "application/json")
  .body("{\n  \"task_id\": \"agent-session-42\"\n}")
  .asString();
```

```php
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.example.com/sandboxes/destroy', [
  'body' => '{
  "task_id": "agent-session-42"
}',
  'headers' => [
    'Authorization' => 'Bearer <token>',
    'Content-Type' => 'application/json',
  ],
]);

echo $response->getBody();
```

```csharp
using RestSharp;

var client = new RestClient("https://api.example.com/sandboxes/destroy");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer <token>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"task_id\": \"agent-session-42\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = [
  "Authorization": "Bearer <token>",
  "Content-Type": "application/json"
]
let parameters = ["task_id": "agent-session-42"] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://api.example.com/sandboxes/destroy")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```