## Clear chat history

`POST /v1/chats/:dialogId/history/clear`

Clear history for the caller with `scope: "self"` or for all participants with `scope: "all"`. The chat is retained. Bitrix24 authorizes the selected operation as the user whose tokens are used in the request.

**Irreversible:** clearing history for everyone removes the shared history. Ask the user to confirm the selected scope before making the request.

## Parameters

| Parameter | Type | Required | Description |
|----------|-----|:-----:|---------|
| `dialogId` (path) | string | yes | Dialog ID from [recent dialogs](/docs/chats/discovery/recent): `chatN`, a positive user ID or `me`. The numeric part is an integer without leading zeros, at most `9007199254740991` |

Query parameters are not accepted.

## Request fields (body)

| Field | Type | Required | Description |
|------|-----|:-----:|---------|
| `scope` | string | yes | `self` clears history for the caller, `all` for everyone. There is no default |

The body must be a JSON object with only the `scope` field. Additional fields are refused before any Bitrix24 call.

## Examples

### curl — personal key

```bash
curl -X POST https://vibecode.bitrix24.com/v1/chats/chat42/history/clear \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"scope":"self"}'
```

### curl — OAuth application

```bash
curl -X POST https://vibecode.bitrix24.com/v1/chats/chat42/history/clear \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"scope":"self"}'
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/chat42/history/clear', {
  method: 'POST',
  headers: { 'X-Api-Key': 'YOUR_API_KEY', 'Content-Type': 'application/json' },
  body: JSON.stringify({ scope: 'self' }),
})
const { success, data } = await res.json()
```

### JavaScript — OAuth application

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/chat42/history/clear', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ scope: 'self' }),
})
const { success, data } = await res.json()
```

## Response fields

| Field | Type | Description |
|------|-----|---------|
| `success` | boolean | `true` when the operation is confirmed |
| `data.result` | boolean | `true` confirms the operation |

## Response example

```json
{
  "success": true,
  "data": {
    "result": true
  }
}
```

## Error response example

400 — `scope` is missing:

```json
{
  "success": false,
  "error": {
    "code": "MISSING_PARAMS",
    "message": "Body field `scope` is required."
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|---------|
| 400 | `MISSING_PARAMS` | The `scope` field is missing |
| 400 | `INVALID_PARAMS` | Malformed `dialogId`, invalid `scope`, wrong body type, extra body field or query parameter |
| 401 | `TOKEN_MISSING` | The API key has no configured Bitrix24 tokens |
| 403 | `SCOPE_DENIED` | The API key has no `im` scope |
| 403 | `WRITE_BLOCKED_READONLY_KEY` | The key access mode forbids changing Bitrix24 data. See [access rights](/docs/access-rights) |
| 403 | `BITRIX_ACCESS_DENIED` | Bitrix24 refused access to the selected operation |
| 404 | `ENTITY_NOT_FOUND` | Bitrix24 returned "not found". The code is in `error.b24Code` |
| 422 | `BITRIX_ERROR` | Bitrix24 returned a refusal. The original code, including `HISTORY_CLEANUP_CONFLICT`, is in `error.b24Code` |
| 502 | `BITRIX_UNAVAILABLE` | No explicit confirmation was received, or Bitrix24 is unavailable |
| 502 | `ME_ALIAS_RESOLUTION_FAILED` | The user could not be resolved for `me` |

For the full list of common API errors, see [Errors](/docs/errors).

## Known specifics

A `502` response may arrive after the operation has changed data. Vibecode does not automatically repeat the write after a Bitrix24 `503` response. Before retrying, check the chat state and obtain a new user confirmation.

## See also

- [Chat management](/docs/chats/management)
- [Delete a chat](/docs/chats/management/delete)
- [Read messages](/docs/chats/messages/list)
