# messages.deleteMany

Empty trash, or delete a batch of messages or whole conversations.

Permanently purges every row on the Recently-deleted tier with state=expunged, or deletes the messages named by `ids` and/or the conversations named by `threadIds`. These are the only two gestures in the API that cannot be undone by moving mail back (`purge=true` destroys; without it the rows drop to the Recently-deleted tier and age out), so a conversation named here MUST also carry `threadLabel`, the folder it was being viewed in. Omitting it is `thread_context_required`, never a default: a conversation routinely has one member in Trash and another still in the Inbox, and a Trash-view delete that expanded to the default set would take the Inbox copy with it. `threadIds` names LIVE conversations only — the Recently-deleted tier has no conversation view, so purging rows there is an `ids` gesture.

```ts
openemail.messages.deleteMany(
  mailboxId: string,
  body?: MessagesDeleteManyBody,
  options?: MessagesDeleteManyOptions,
): ApiPromise<MessagesDeleteManyResult>
```

It calls `DELETE /mailboxes/{mailboxId}/messages`: [Empty trash, or delete a batch of messages or whole conversations](/docs/api/messages/delete_MessageEmptyTrash) in the API reference.

## Example

```ts
const result = await openemail.messages.deleteMany(mailboxId, {})
```

## Arguments

| Argument | Type | Description |
| --- | --- | --- |
| `mailboxId` | `string` | Mailbox ULID identifier. The path's `{mailboxId}`. |
| `body` | [`MessagesDeleteManyBody`](#messagesdeletemanybody) | Optional. The request's body. |

## Options

The last argument, `options`, a `MessagesDeleteManyOptions`, takes:

| Option | Type | Sent as | Description |
| --- | --- | --- | --- |
| `state` | `'expunged'` | query `state` | Set to 'expunged' to permanently purge all trash messages |
| `purge` | `boolean` | query `purge` | When deleting by IDs, set to 'true' to permanently purge instead of expunging to trash |

Every call also takes `signal`, `timeout`, `retry` and `headers`, which say how that one call is sent ([call options](/docs/sdks/typescript/calling-methods#call-options)).

## Answer

Awaited, it is the API's JSON, [`MessagesDeleteManyResult`](#messagesdeletemanyresult). It is an [`ApiPromise`](/docs/sdks/typescript/responses): `.withResponse()` gives the same with the `Response` it came in, for its headers.

## Errors

| Status | What it means |
| --- | --- |
| `400` | validation_failed, delete_target_required, delete_target_exclusive, thread_context_required, unknown_label, or thread_too_large |
| `401` | unauthorized: the bearer credential is missing, malformed, expired, or revoked. Carries a `WWW-Authenticate: Bearer` challenge |
| `404` | Mailbox not found or not accessible to this principal |
| `413` | too_large: JSON request body exceeds 1 MiB or 500,000 values (object keys included), checked before parsing |
| `503` | temporary_failure: the platform's storage could not answer just now; retry after the Retry-After window. Any operation can answer this. A write that got it may have been applied, so retry it with the same idempotency key where it takes one, or read the state back first (see Transient failures in the API description) |

A failed call rejects with a [`RequestError`](/docs/sdks/typescript/errors): its `status`, and its `code`, the API's `error`. Decide on the code: one status covers several.

## Types

### MessagesDeleteManyBody

```ts
/** The body of `messages.deleteMany`. */
type MessagesDeleteManyBody = {
  /** Array of message IDs to delete */
  ids?: string[]
  /** Roles to exclude from thread expansion (e.g. ['sent', 'drafts']). Members carrying any label with one of these roles are omitted from the expanded conversation. Trash and Junk members are already left out by the default expansion, so naming them here changes nothing unless `threadLabel` is that folder, where it empties the set. Explicit `ids` are unaffected. If every member is excluded, the conversation contributes nothing, which is not an error. */
  threadExcludeRoles?: ('inbox' | 'sent' | 'drafts' | 'trash' | 'archive' | 'junk')[]
  /** Conversation IDs to delete, expanded server-side to the members visible in `threadLabel` — which is REQUIRED alongside this. Refused with thread_too_large past 1000 expanded messages rather than deleting part of a conversation. */
  threadIds?: string[]
  /** The folder each conversation in `threadIds` was being viewed in — the same value passed to GET /threads/{threadId}?label=, so the delete reaches exactly the members that view rendered. Inside Trash that is the trashed members, inside Junk the junked ones; anywhere else it is the live members carrying neither label. Required with `threadIds`, and validated: an unknown folder is unknown_label, not a fall back to the default set. */
  threadLabel?: string
  /** Narrow each conversation to the members FILED UNDER `threadLabel`, as on PATCH /messages. Without it a delete made in the Inbox also takes the Sent copy of your replies and any member filed elsewhere — they are all part of the conversation that view renders. Inside Trash it changes nothing: the trashed members are already exactly that set. */
  threadLabelOnly?: boolean
}
```

### MessagesDeleteManyResult

```ts
/** What `messages.deleteMany` answers. */
type MessagesDeleteManyResult = EmptyTrashResult | BatchDeleteResult
```

It names these types of the API, each on a page of its own: [`BatchDeleteResult`](/docs/sdks/typescript/types/batch-delete-result), [`EmptyTrashResult`](/docs/sdks/typescript/types/empty-trash-result).
