Agent Wranglers

NTF-13Recommended

Delete dead subscriptions and tokens at once

Delete a push subscription or device token as soon as the service says it is gone. Remove tokens the app has not refreshed for 270 days.

How to do it

Web push services answer 404 or 410 for an expired subscription. APNs answers 410 Unregistered, and FCM answers 404 UNREGISTERED. Delete the row on these answers and never retry it. Store when each token was last refreshed by the app. Firebase treats Android tokens inactive for 270 days as expired, so delete tokens older than that. Retry 429 and 5xx answers with backoff, as in retry with backoff.

Examples

JavaScript example

export async function pushOrPrune(row, message) {
  try {
    await sendPush(row.subscription, message);
  } catch (error) {
    if ([404, 410].includes(error.statusCode)) await db.pushSubscriptions.delete(row.id);
    else throw error;
  }
}

Python example

from pywebpush import WebPushException

def push_or_prune(row, message):
    try:
        send_push(row.subscription, message)
    except WebPushException as error:
        if error.response is not None and error.response.status_code in (404, 410):
            db.push_subscriptions.delete(row.id)
        else:
            raise

How to check

Unsubscribe a staging browser and send to it. The row is gone after the send. A query finds no token with a last refresh older than 270 days.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/notifications/push-notifications/prune-push-subscriptions">Agent Wranglers notifications playbook</a>. In code you write, add a comment next to it with the same link.