Skip to content
Software capabilities
Gmail

Recover a stale Gmail history ID

Use history.list while the saved startHistoryId is valid. If an outdated ID causes a 404, rebuild the local mailbox index and save the historyId of the most recent message from the full sync.

Documentation reviewed · 3 methods with sources

Ways to do it

  1. API: GET /gmail/v1/users/{userId}/history
  2. API: Full sync after HTTP 404
  3. SDK: gmail.users().history().list()
API

GET /gmail/v1/users/{userId}/history

Read mailbox changes after startHistoryId. Follow nextPageToken and save the returned historyId only after processing the last page.

Read source: Gmail users.history.list reference
Requirements
Gmail OAuth credentials with an appropriate readonly, metadata, or modify scope and a stored history ID.
Limitations
History IDs increase with gaps. Google says they usually remain valid for at least a week and can expire within hours in rare cases.

Example shell

curl --fail --get 'https://gmail.googleapis.com/gmail/v1/users/me/history' -H "Authorization: Bearer $GOOGLE_ACCESS_TOKEN" --data-urlencode "startHistoryId=$GMAIL_HISTORY_ID"
API

Full sync after HTTP 404

List messages, fetch the fields your app uses, and save the historyId from the most recent message after rebuilding.

Read source: Gmail synchronize clients guide
Requirements
A Gmail OAuth token with scope for the fields your app reads and local storage for the mailbox index.
Limitations
Rebuild the subset of the mailbox your app tracks. If it caches message bodies, request FULL or RAW on the initial messages.get calls.

Example text

1. List the messages your app tracks with messages.list.
2. Fetch each record with messages.get and rebuild the local index.
3. Save the historyId from the most recent message after committing the rebuild.
SDK

gmail.users().history().list()

Use the Python client with an authenticated Gmail service to read one page of changes.

Read source: Gmail Python client history.list
Requirements
An authenticated Gmail API service from google-api-python-client and a saved history ID.
Limitations
Reuse the original startHistoryId on subsequent pages. A stale ID requires the same full-sync recovery as REST.

Example text

page = gmail.users().history().list(
    userId="me", startHistoryId=saved_history_id
).execute()