# Delete an asset

> Deletes a file from the library and frees its storage. A file templates still use needs force=true.

<Endpoint method="DELETE" path="/v1/assets/:id" />

<CodeTabs>

```bash tab="cURL"
curl -X DELETE https://api.muzekit.com/v1/assets/ast_dQC3Z8RgqAH \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY"
```

```javascript tab="Node"
const res = await fetch("https://api.muzekit.com/v1/assets/ast_dQC3Z8RgqAH", {
  method: "DELETE",
  headers: { Authorization: `Bearer ${process.env.MUZEKIT_SECRET_KEY}` },
});
const deleted = await res.json();
```

```python tab="Python"
import os
import requests

deleted = requests.delete(
    "https://api.muzekit.com/v1/assets/ast_dQC3Z8RgqAH",
    headers={"Authorization": f"Bearer {os.environ['MUZEKIT_SECRET_KEY']}"},
).json()
```

</CodeTabs>

Add `user=usr_…` for a file in that end user's library.

Deleting a file frees its storage, and the file stops loading everywhere. If templates still use it, the request is refused with `409 asset_in_use`, saying how many templates and naming the first few; deleting it would break them. Add `force=true` to delete it anyway. Any URL to the file you saved elsewhere stops working too.

```json
{
  "error": {
    "type": "conflict_error",
    "code": "asset_in_use",
    "message": "This file is used in 2 templates (\"Spring sale\" and \"Menu card\"). Deleting it will break them: the file goes missing. Delete anyway with force.",
    "docs_url": "https://muzekit.com/docs/errors#asset_in_use",
    "templates": 2,
    "names": [ "Spring sale", "Menu card" ]
  }
}
```

Templates in the trash count too, since they can still be restored. With `user`, `templates` counts every template that uses the file, but `names` lists only that user's designs and workspace templates.

## Examples

### Delete a file from an end user's library

Pass the same `user` the file was uploaded with. Without it, the API looks in the workspace library and answers `404 asset_not_found`.

<CodeTabs>

```bash tab="cURL"
curl -X DELETE "https://api.muzekit.com/v1/assets/ast_dQC3Z8RgqAH?user=usr_8fK2mQ1xZpR" \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY"
```

```javascript tab="Node"
const url =
  "https://api.muzekit.com/v1/assets/ast_dQC3Z8RgqAH?user=usr_8fK2mQ1xZpR";
const res = await fetch(url, {
  method: "DELETE",
  headers: { Authorization: `Bearer ${process.env.MUZEKIT_SECRET_KEY}` },
});
const deleted = await res.json();
```

```python tab="Python"
import os
import requests

deleted = requests.delete(
    "https://api.muzekit.com/v1/assets/ast_dQC3Z8RgqAH",
    params={"user": "usr_8fK2mQ1xZpR"},
    headers={"Authorization": f"Bearer {os.environ['MUZEKIT_SECRET_KEY']}"},
).json()
```

</CodeTabs>

### Ask before deleting a file in use

Try without `force` first. On `409 asset_in_use`, show the person the count and names, and send the request again with `force=true` only if they confirm.

<CodeTabs>

```bash tab="cURL"
# 1. Refused with 409 asset_in_use if templates use the file
curl -X DELETE "https://api.muzekit.com/v1/assets/ast_dQC3Z8RgqAH?user=usr_8fK2mQ1xZpR" \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY"

# 2. After the person confirms
curl -X DELETE "https://api.muzekit.com/v1/assets/ast_dQC3Z8RgqAH?user=usr_8fK2mQ1xZpR&force=true" \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY"
```

```javascript tab="Node"
async function deleteAsset(id, user, force = false) {
  const params = new URLSearchParams({ user });
  if (force) params.set("force", "true");
  const res = await fetch(`https://api.muzekit.com/v1/assets/${id}?${params}`, {
    method: "DELETE",
    headers: { Authorization: `Bearer ${process.env.MUZEKIT_SECRET_KEY}` },
  });
  return { status: res.status, body: await res.json() };
}

let result = await deleteAsset("ast_dQC3Z8RgqAH", "usr_8fK2mQ1xZpR");
if (result.status === 409 && result.body.error.code === "asset_in_use") {
  const { templates, names } = result.body.error;
  // Show templates and names; continue only if the person confirms
  result = await deleteAsset("ast_dQC3Z8RgqAH", "usr_8fK2mQ1xZpR", true);
}
```

```python tab="Python"
import os
import requests

def delete_asset(asset_id, user, force=False):
    params = {"user": user}
    if force:
        params["force"] = "true"
    return requests.delete(
        f"https://api.muzekit.com/v1/assets/{asset_id}",
        params=params,
        headers={"Authorization": f"Bearer {os.environ['MUZEKIT_SECRET_KEY']}"},
    )

res = delete_asset("ast_dQC3Z8RgqAH", "usr_8fK2mQ1xZpR")
if res.status_code == 409 and res.json()["error"]["code"] == "asset_in_use":
    error = res.json()["error"]
    # Show error["templates"] and error["names"]; continue only if confirmed
    res = delete_asset("ast_dQC3Z8RgqAH", "usr_8fK2mQ1xZpR", force=True)
```

</CodeTabs>

### Remove all of an end user's uploads

When a user leaves your product, list their files and delete each one with `force=true`. A list returns up to 200 files, so repeat until it comes back empty.

<CodeTabs>

```bash tab="cURL"
USER_ID=usr_8fK2mQ1xZpR

for ID in $(curl -s "https://api.muzekit.com/v1/assets?user=$USER_ID" \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY" | jq -r '.data[].id'); do
  curl -s -X DELETE "https://api.muzekit.com/v1/assets/$ID?user=$USER_ID&force=true" \
    -H "Authorization: Bearer $MUZEKIT_SECRET_KEY"
done
```

```javascript tab="Node"
const user = "usr_8fK2mQ1xZpR";
const headers = { Authorization: `Bearer ${process.env.MUZEKIT_SECRET_KEY}` };
const base = "https://api.muzekit.com/v1/assets";

const { data } = await (await fetch(`${base}?user=${user}`, { headers })).json();
for (const asset of data) {
  await fetch(`${base}/${asset.id}?user=${user}&force=true`, {
    method: "DELETE",
    headers,
  });
}
```

```python tab="Python"
import os
import requests

user = "usr_8fK2mQ1xZpR"
headers = {"Authorization": f"Bearer {os.environ['MUZEKIT_SECRET_KEY']}"}
base = "https://api.muzekit.com/v1/assets"

assets = requests.get(base, params={"user": user}, headers=headers).json()["data"]
for asset in assets:
    requests.delete(
        f"{base}/{asset['id']}",
        params={"user": user, "force": "true"},
        headers=headers,
    )
```

</CodeTabs>

## Response

```json
{ "object": "asset", "id": "ast_dQC3Z8RgqAH", "deleted": true }
```
