# search.updateMatches

Update flags, labels and keywords for every message a search selects.

Applies one patch to every message `POST /search/query` would return for the same `filter` — the way to act on a whole result set without naming its messages. The filter grammar, and the set it selects, are that route's own.

SEND BACK THE `excludeRoles` YOU SEARCHED WITH. It narrows the selection here exactly as it narrows the results there, and it is not part of the filter — so a call that omits it patches the Trash and Junk mail the search left out.

THE CEILING. `idMax` is a message id (a ULID, so ordered by arrival) resolved on the first call and echoed back: mail that arrives while the gesture runs has a later id and is never touched. Send it back unchanged on every continuation.

IT MAY ANSWER A CURSOR, exactly as `PATCH /labels/{label}/messages` does: the call spends a bounded budget, and a non-null `nextAfter` means call again with `after` set to it, the same `idMax`, the same filter and the same patch. Unlike that route it reports no `remaining` — measure progress against the search's own `total`.

A FULL-TEXT filter is bounded: one that matches more than 5,000 messages is refused with `too_large`, having written nothing, rather than applied to some of them. Narrow the search. A filter is REQUIRED — this route will not patch a whole mailbox by omission.

```ts
openemail.search.updateMatches(
  mailboxId: string,
  body?: SearchUpdateMatchesBody,
  options?: CallOptions,
): ApiPromise<QueryMessagesUpdateResult>
```

It calls `PATCH /mailboxes/{mailboxId}/search/query`: [Update flags, labels and keywords for every message a search selects](/docs/api/messages/patch_MailboxSearchQueryUpdate) in the API reference.

## Example

```ts
const queryMessagesUpdateResult = await openemail.search.updateMatches(mailboxId, {})
```

## Arguments

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

## Options

It takes no options of its own. 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, [`QueryMessagesUpdateResult`](/docs/sdks/typescript/types/query-messages-update-result). 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, filter_required, patch_required, idmax_required (after without idMax), too_large (a full-text filter over the cap), unsupported_filter, invalid_filter, unknown_label (a name in the patch), or invalid_keyword |
| `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

### SearchUpdateMatchesBody

```ts
/** The body of `search.updateMatches`. */
type SearchUpdateMatchesBody = {
  /** Continuation cursor: pass the previous call's `nextAfter`. */
  after?: string
  /** System label roles whose mail this query must not return — e.g. `["trash", "junk"]` for an All-Mail view. A message carrying ANY label with one of these roles is excluded, whatever else it carries. Says in one argument what a filter would need a label lookup and one bound value per label to say; a role no label answers to is simply not excluded. */
  excludeRoles?: ('inbox' | 'sent' | 'drafts' | 'trash' | 'archive' | 'junk')[]
  filter?: EmailSearchFilter
  /** Flags to remove */
  flagsClear?: ('seen' | 'answered' | 'flagged' | 'draft' | 'deleted')[]
  /** Flags to set */
  flagsSet?: ('seen' | 'answered' | 'flagged' | 'draft' | 'deleted')[]
  /** ULID ceiling — the selection is every match with id <= this. Omitted on a FIRST call; REQUIRED once `after` is given. */
  idMax?: string
  /** Custom keywords to remove */
  keywordsClear?: string[]
  /** Exact replacement list of custom keywords */
  keywordsExact?: string[]
  /** Custom keywords to add */
  keywordsSet?: string[]
  /** Labels to add */
  labelsAdd?: string[]
  /** Exact replacement list of labels */
  labelsExact?: string[]
  /** Labels to remove */
  labelsRemove?: string[]
  /** Lower the per-call update budget. Never raises it. */
  limit?: number
  /** What to do if this write would leave the message with no label. 'expunge' (the default) sends the message to Recently deleted. 'archive' keeps the message live by attaching the Archive system label (attaching Archive needs no right beyond the grant's 't' for a scoped viewer). If the write removes Archive itself and would leave no labels, Archive is kept without error. A SCOPED viewer whose write lands the message in Archive has moved it out of their own scope: the answer is still `updated`, and the hydrated message carries an EMPTY `labels` list rather than naming a label the grant never included. `labelsAdded` omits Archive for the same reason. */
  orphans?: ('expunge' | 'archive')
}
```

It names these types of the API, each on a page of its own: [`EmailSearchFilter`](/docs/sdks/typescript/types/email-search-filter), [`QueryMessagesUpdateResult`](/docs/sdks/typescript/types/query-messages-update-result).
