---
title: "IMAP sync: UIDVALIDITY, CONDSTORE, IDLE · Workwae"
description: "How to sync IMAP mailboxes without push: UIDVALIDITY resets, UIDNEXT, CONDSTORE and QRESYNC, IDLE limits, connection caps, special-use folders and Sent copies."
url: https://workwae.com/blog/imap-sync-uidvalidity-condstore-idle
---

Blog

# Syncing IMAP without a push API: UIDVALIDITY, CONDSTORE and IDLE.

IMAP has no [change feed](https://workwae.com/glossary#change-feed), so a client keeps its own state per folder and has to know when that state stops being valid.

November 30, 2026 · 6 min read

- [IMAP](https://workwae.com/blog/tag/imap)
- [Sync and push](https://workwae.com/blog/tag/sync)

Gmail and Microsoft 365 give you a change feed and push. A plain IMAP server gives you a connection, a folder list and some numbers. What changed since the last visit, whether the stored numbers still mean anything, and how to hear about new mail are up to the client.

This article covers what a sync engine depends on, from the RFCs and provider documentation: UIDs, UIDVALIDITY, UIDNEXT, CONDSTORE, QRESYNC, IDLE, connection limits, special-use folders and sending. Then it shows how Workwae’s IMAP sync uses them, and where it falls short.

## A message is a mailbox, a UIDVALIDITY and a UID.

Message sequence numbers shift when anything is expunged, so they are useless between sessions. The stable name is the unique identifier, the UID. UIDs are strictly ascending in a mailbox, and an expunged UID is never reused under the same UIDVALIDITY value ([RFC 9051, section 2.3.1.1](https://www.rfc-editor.org/rfc/rfc9051#section-2.3.1.1); the same rules are in [RFC 3501](https://www.rfc-editor.org/rfc/rfc3501#section-2.3.1.1)).

UIDVALIDITY is the other half of the name. If a server cannot keep UIDs stable, it must send a UIDVALIDITY value greater than the one it used before. The RFC says the mailbox name, UIDVALIDITY and UID together refer to a single immutable message forever. A client that stores only the UID is storing a name that can silently point at a different message.

## A new UIDVALIDITY discards every UID you stored.

The value arrives in an untagged `OK [UIDVALIDITY n]` response when you select a mailbox. The same RFC says a server that deletes a mailbox and later creates one with the same name must either keep the old UIDs or assign a new UIDVALIDITY. So a changed value is not an error. It is the server telling you to forget the mailbox.

What it forces is a full re-read. Nothing cached by UID can be trusted, including flags and the mapping from UID to your own message ids. The costly part is deciding which re-read messages you already knew, so a reset does not look like a mass delete and a mass arrival. The usual answer is to match on content: [Message-ID](https://workwae.com/glossary#message-id), internal date and size. Per mailbox, a client keeps three numbers and compares them on every visit:

per-mailbox state (example)

```json
{
  "INBOX": {
    "uidValidity": 1791521224,
    "uidNext": 4393,
    "highestModseq": 88213
  }
}
```

## UIDNEXT answers whether mail arrived.

UIDNEXT is the UID the next new message is predicted to get. It must not change unless messages are added, and it must change whenever one is added, even if that message is expunged later. The RFC notes it exists so a client can tell whether anything was delivered since the last check ([RFC 9051](https://www.rfc-editor.org/rfc/rfc9051#section-2.3.1.1)). With the stored value, new mail is every UID at or above it, and a `STATUS` command can read it without selecting the mailbox.

UIDNEXT says nothing about the rest. A message marked read, flagged or deleted does not move it, and neither does an expunge. Those changes need one of two approaches.

## Flags and expunges need CONDSTORE, or a full comparison.

Without extensions, a client reads the flags of every message it holds and lists the mailbox’s UIDs to see which are gone. That is a cost proportional to the mailbox on every visit. CONDSTORE ([RFC 7162](https://www.rfc-editor.org/rfc/rfc7162)) adds a per-mailbox counter, HIGHESTMODSEQ, that rises with every change. A client stores it, then asks for only what changed with the `CHANGEDSINCE` modifier on `FETCH`. Servers that cannot persist mod-sequences for a mailbox answer `NOMODSEQ` instead, and the client must fall back.

CONDSTORE reports changed flags but not expunged messages. The RFC says the client still has to issue a `UID FETCH` or `UID SEARCH` to find them. QRESYNC, defined in the same document, closes that gap. The client sends `ENABLE QRESYNC` once, then selects with the UIDVALIDITY and mod-sequence it last saw. The server replies with the changes and lists expunged UIDs in `VANISHED (EARLIER)`. If the UIDVALIDITY no longer matches, the server sends the new value and none of the changes, and the client re-reads the mailbox:

QRESYNC select (example)

```text
C: a1 ENABLE QRESYNC
S: * ENABLED QRESYNC
S: a1 OK
C: a2 SELECT INBOX (QRESYNC (1791521224 88213))
S: * OK [UIDVALIDITY 1791521224]
S: * OK [UIDNEXT 4395]
S: * OK [HIGHESTMODSEQ 88240]
S: * VANISHED (EARLIER) 4390:4391
S: * 22 FETCH (UID 4388 FLAGS (\Seen) MODSEQ (88231))
S: a2 OK [READ-WRITE] SELECT completed
```

## IDLE watches one mailbox on one connection.

`IDLE` ([RFC 2177](https://www.rfc-editor.org/rfc/rfc2177), folded into [RFC 9051](https://www.rfc-editor.org/rfc/rfc9051#section-6.3.13)) tells the server the client will accept unsolicited `EXISTS`, `EXPUNGE` and `FETCH` responses until it sends `DONE`. Those responses describe the selected mailbox, and only one mailbox can be selected at a time in a connection ([RFC 9051, SELECT](https://www.rfc-editor.org/rfc/rfc9051#section-6.3.2)). Watching ten folders this way takes ten connections.

A server with an inactivity timeout may log out a client that sits in IDLE. The RFC advises ending IDLE and issuing it again at least every 29 minutes. Notifications are also not a guarantee: a connection that dropped quietly delivers nothing, and a client has to keep a poll as a backstop. NOTIFY ([RFC 5465](https://www.rfc-editor.org/rfc/rfc5465)) lets one connection hear about other mailboxes, but servers are not required to offer it.

## Connection limits are per account and low.

Every watched folder is another login, and servers cap them. Google says an account can be added to up to 15 email clients at a time, and past that Gmail answers “Too many simultaneous connections” ([Gmail Help](https://support.google.com/mail/answer/7126229)). Dovecot, a common self-hosted server, limits IMAP connections per user and IP address with `mail_max_userip_connections`, which defaults to 10 ([Dovecot limits](https://doc.dovecot.org/main/core/admin/limits.html)).

## Find Sent and Trash by attribute, not by name.

Folder names vary by server and language. RFC 6154 adds special-use attributes to `LIST` responses, among them `\Sent`, `\Drafts` and `\Trash`. Its example server marks a folder named `SentMail` as `\Sent` ([RFC 6154](https://www.rfc-editor.org/rfc/rfc6154)), which a client looking for “Sent” would miss. Servers that send no attributes still need a fallback.

## SMTP sends, and nothing files the copy.

IMAP never sends. Mail clients that read over IMAP rely on SMTP to send, and most can be configured to keep a copy of sent items on the server ([Microsoft Learn, POP3 and IMAP4 in Exchange Online](https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/pop3-and-imap4/pop3-and-imap4)). On a server that does not file sent mail itself, that copy is an `APPEND` to the Sent folder, with `\Seen` so it does not show as unread. Skip it and Sent never shows the message. Do it on a server that files its own copy and Sent holds two. [Sending mail as your users](https://workwae.com/blog/sending-mail-as-your-users) covers who writes the Sent copy across SMTP, Gmail and Graph.

## How Workwae handles it.

- **Two connections at most.** Per account, one stays logged in on INBOX and sits in IDLE. A second one reads every other folder and logs out 30 seconds after its last use.
- **State per folder.** The sync saves UIDVALIDITY, UIDNEXT, HIGHESTMODSEQ and the message count after each pass, in the same transaction as the last changes the pass found. A pass commits in batches of 1,000 messages. A pass that fails keeps what it committed, and the next one resumes from there.
- **QRESYNC when offered.** Workwae selects with the saved UIDVALIDITY and mod-sequence, reads `VANISHED (EARLIER)` for expunges, and fetches flags with `CHANGEDSINCE`. UIDs at or above the old UIDNEXT are new messages. Without QRESYNC it lists the folder’s UIDs, diffs them against the index and reads the flags of the indexed ones.
- **A UIDVALIDITY change re-reads the folder.** The re-read is diffed against the index. Indexed messages are matched to the re-read ones by Message-ID, internal date and size, and keep their ids, so your webhooks see only real differences.
- **IDLE on INBOX, a poll for the rest.** A full pass runs every 5 minutes by default, and an API write or a restart starts one at once. On a server with NOTIFY, other folders report changes as they happen. Without it, the IDLE connection leaves IDLE every minute by default, compares a STATUS of Sent and Drafts with the last pass, and goes back. A folder reported changed whose STATUS still matches the last pass is not read again.
- **Roles from attributes.** Roles come from the server’s special-use attributes. The IMAP library adds well-known names only for a role no attribute claimed. Sent, Drafts and Trash are found by role.
- **Sending and filing.** Mail goes out through the account’s SMTP server. With `appendSent``append_sent` on, Workwae appends the copy to the Sent folder with `\Seen`. The send is already out by then, so a failure to append is logged and does not fail the send. `appendSent``append_sent` defaults to off when the IMAP host is Google’s or Microsoft’s, which file the copy themselves, and on otherwise. After a send on an IMAP account, the sync runs at once to index the Sent copy.

The sync window and the Sent behavior are set when the account is created:

imap-account.tsimap-account.py

```ts
import { createClient } from "@workwae/sdk";

const workwae = createClient({ apiKey: process.env.WORKWAE_API_KEY! });

const account = await workwae.imapAccounts.create({
  email: "ada@example.com",
  password: "app-password",
  imap: { host: "imap.example.com", port: 993, security: "tls" },
  smtp: { host: "smtp.example.com", port: 587, security: "starttls" },
  appendSent: true,
  settings: { syncWindowDays: 30 }, // default is 90; null reads the whole mailbox
});
```

```python
import os

from workwae import Workwae

workwae = Workwae(api_key=os.environ["WORKWAE_API_KEY"])

account = workwae.imap_accounts.create(
    email="ada@example.com",
    password="app-password",
    imap={"host": "imap.example.com", "port": 993, "security": "tls"},
    smtp={"host": "smtp.example.com", "port": 587, "security": "starttls"},
    append_sent=True,
    settings={"sync_window_days": 30},  # default is 90; None reads the whole mailbox
)
```

The window limits which messages are added; one already indexed is followed wherever it goes. The first sync ends with `account.initialized`. After that, a new, changed or expunged message arrives as `message.created`, `message.updated` or `message.deleted`, and your backend never sees a UID.

## Limits.

- **Outside INBOX, latency is the poll’s.** Without NOTIFY, only INBOX is live. Sent and Drafts are checked every minute and other folders wait for the poll, as do flag changes without HIGHESTMODSEQ. The poll is a deployment setting of 30 to 300 seconds, and defaults to 300.
- **Servers without CONDSTORE or QRESYNC cost more.** Every pass of a changed folder lists its UIDs and reads the flags of every indexed message in it.
- **Moves are two events without OBJECTID.** Without [OBJECTID](https://www.rfc-editor.org/rfc/rfc8474) or Gmail’s X-GM-MSGID, a move made by another mail program is a delete in one folder and a create in the other.
- **No timed IDLE restart.** Workwae does not re-issue IDLE on a 29-minute timer. IDLE ends when a pass runs commands on the connection, at least every poll interval, and is restarted after. A server that drops the connection fails the sync, which retries after 30 seconds, doubling up to 15 minutes.
- **Two connections come out of the provider’s budget.** The user’s other clients share the 15 on Gmail or Dovecot’s default of 10.
- **[CalDAV](https://workwae.com/glossary#caldav) and [CardDAV](https://workwae.com/glossary#carddav) have no push.** They are polled on their own interval.
- **Alpha SDKs.** `@workwae/sdk` is at 0.1.0-alpha and `workwae` at 0.1.0a2.

[The IMAP provider page](https://workwae.com/providers/imap) lists what an IMAP account supports. The [IMAP and SMTP guide](https://docs.workwae.com/guides/imap) shows how to connect one, and the [sync window section](https://docs.workwae.com/guides/mail#sync) of the mail guide describes the initial sync.

## More from the blog.

### [Gmail API vs IMAP: authorization, sync, quotas and labels.](https://workwae.com/blog/gmail-api-vs-imap)

November 18, 2026 · 8 min read

Scopes and CASA, XOAUTH2 and app passwords, history and Pub/Sub against UIDs and IDLE, quota units against bandwidth, and when each fits.

Tags: Gmail, Sync and push, OAuth

### [Email and calendar webhooks you can trust.](https://workwae.com/blog/email-webhooks-you-can-trust)

October 26, 2026 · 7 min read

Signatures, duplicates, ordering, retries, replay and secret rotation for mail and calendar webhooks.

Tags: Webhooks, Sync and push

### [Microsoft Graph change notifications for mail and calendar sync.](https://workwae.com/blog/microsoft-graph-change-notifications)

October 12, 2026 · 6 min read

Subscriptions, validation, lifecycle events and why delta queries, not notifications, are the record.

Tags: Microsoft Graph, Sync and push, Webhooks
