Prompt
How do I handle invalid push tokens and cleanup automatically?
Latest observation
Oct 14, 2026
OpenAI APIWeb search: off
To handle invalid push tokens automatically, you usually want two layers of cleanup:
- Detect invalid tokens from the push provider response
- Remove or deactivate those tokens in your database right away
1) Treat push responses as the source of truth
When you send a notification, providers like FCM, APNs, or Expo may return errors such as:
- token is invalid / not registered
- device token is expired
- malformed token
- permission denied / unregistered
If a token fails with a permanent error, mark it as unusable and delete it.
Examples of permanent invalid-token signals
- FCM:
registration-token-not-registered,invalid-registration-token - APNs:
BadDeviceToken,Unregistered - Expo: receipts can show
DeviceNotRegistered
2) Remove bad tokens immediately
A common pattern:
- Store push tokens in a table/collection with fields like:
tokenuserIdplatformlastSeenAtactiveinvalidAt
- On send:
- if provider returns a permanent error, set
active = falseor delete the token - keep a log for debugging if needed
- if provider returns a permanent error, set
3) Run periodic cleanup jobs
Even with immediate cleanup, it’s good to run a scheduled job to catch stale tokens:
- remove tokens not seen in a long time
- retry transient failures a limited number of times
- disable tokens that repeatedly fail
Example cleanup policy:
- if token hasn’t been refreshed in 90 days, flag it stale
- if it fails permanently once, deactivate immediately
- if it fails transiently 3 times in a row, alert or temporarily suppress
4) Update tokens whenever the app starts
Clients should resend their push token on:
- app launch
- token refresh event
- user login
- permission changes
This helps replace old tokens before they go stale.
5) Keep a send pipeline that batches and prunes
When sending to many tokens:
- batch requests
- inspect each response individually
- prune invalid tokens from the same job
- don’t keep retrying permanently invalid ones
Example workflow
- App registers token and sends it to your backend
- Backend stores token as active
- Backend sends notification later
- Provider says token is invalid
- Backend marks token inactive / deletes it
- Scheduled job also removes stale inactive tokens periodically
Practical recommendation
The most robust setup is:
- Immediate deletion on permanent provider errors
- Scheduled cleanup for stale tokens
- Client-side token refresh on app launch/token change
If you want, I can also show:
- a database schema
- a Node.js/Express example
- or examples for FCM, APNs, or Expo specifically.