Invalid cursor error (#613) when paginating large datasets with Graph API cursor pagination
0 reputation · 13 Oct 2021, 20:39 UTC
0 reputation · 13 Oct 2021, 20:39 UTC
Bound a Graph API query and paginate through a large result set using cursor-based pagination without encountering the (#613) Invalid cursor error.
Cursor tokens expire or become malformed after a short window, and the API does not disclose the total result count, making it difficult to estimate progress. Ordering stability is not guaranteed when the underlying index changes, which can introduce duplicates or gaps across pages. Aggressive limit values risk rate limiting, while very small limits increase request overhead and the chance of cursor expiration.
after cursor token, and can it be refreshed without restarting the query?33275 reputation · 14 Oct 2021, 07:36 UTC
The (#613) Invalid cursor error typically occurs when a pagination token is no longer valid due to expiration, data mutation, or a shift in the underlying dataset order. In cursor-based systems, the token is a pointer to a specific point in the result set; if the database re-indexes or the session-side cache for that cursor times out, the pointer becomes orphaned.
While the API documentation may not explicitly state the lifetime of a cursor, the error usually stems from three scenarios:
createdDateTime without a secondary ID), new records inserted into the collection mid can cause the cursor to skip items or point to an invalid index.To successfully paginate through large datasets without hitting the #613 error, follow these implementation patterns:
Always ensure your orderby clause includes a unique identifier as a tie-breaker. This ensures that even if multiple records share the same timestamp, the cursor has a mathematically stable position.
GET /v1.0/messages?$select=id,subject,receivedDateTime&orderby=receivedDateTime desc,id&$top=100
Instead of querying the entire collection in one stream, break the request into logical buckets. This limits the time any single cursor must remain active and reduces the impact of a failure.
createdDateTime to process data in 24-hour chunks.Since cursors cannot be "refreshed," your client must implement a checkpoint mechanism. When a #613 error is caught:
id gt ''{last_id}'').To provide a more specific recovery algorithm, please clarify: Are you currently sorting by a unique field, or by a non-unique timestamp?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.