# MongoServerError: The resume token for namespace 'mydb.mycoll' is invalid or has been pruned: resumeAfter timestamp too old

- **ID:** `mongodb/change-stream-resume-token-invalid-for-namespace`
- **Domain:** mongodb
- **Category:** protocol_error
- **Verification:** ai_generated
- **Fix Rate:** 70%

## Root Cause

A change stream's resume token refers to an oplog entry that has been pruned due to the oplog size limit or because the change stream was idle longer than the oplog window (typically 24 hours on Atlas).

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| MongoDB 5.0 | active | — | — |
| MongoDB 6.0 | active | — | — |
| MongoDB 7.0 | active | — | — |
| Atlas M10+ | active | — | — |

## Workarounds

1. **Start a new change stream from the current time and accept data loss of missed events. In code: `collection.watch([], { resumeAfter: null, startAfter: null })` or use `startAtOperationTime` with a recent timestamp.** (70% success)
   ```
   Start a new change stream from the current time and accept data loss of missed events. In code: `collection.watch([], { resumeAfter: null, startAfter: null })` or use `startAtOperationTime` with a recent timestamp.
   ```
2. **If using Atlas, increase the oplog window by upgrading cluster tier or enabling long-term oplog retention (if available). For self-managed, increase oplog size: `db.adminCommand({ replSetResizeOplog: 1, size: 102400 })` (in MB).** (80% success)
   ```
   If using Atlas, increase the oplog window by upgrading cluster tier or enabling long-term oplog retention (if available). For self-managed, increase oplog size: `db.adminCommand({ replSetResizeOplog: 1, size: 102400 })` (in MB).
   ```
3. **Implement a fallback mechanism: store the last processed document ID in an external store (e.g., Redis) and use `startAfter` with the last known _id to catch up, then switch to resume tokens.** (75% success)
   ```
   Implement a fallback mechanism: store the last processed document ID in an external store (e.g., Redis) and use `startAfter` with the last known _id to catch up, then switch to resume tokens.
   ```

## Dead Ends

- **** — The resume token is already invalid; a new change stream must be started from the current time, losing all events between the old token and now. (90% fail)
- **** — The oplog entry is permanently pruned; retrying will always fail. (100% fail)
- **** — The error is server-side; no driver can recover a pruned oplog entry. (95% fail)
