Hello Stian Sandve,
Welcome to Microsoft Q&A and Thank you for reaching out.
I understand that you’re experiencing an issue in Azure IoT Hub where GetNextAsTwinAsync (query-based twin retrieval) is returning outdated device twins, while GetTwinAsync (direct twin read) always returns the latest state. This mismatch can understandably create bugs and inconsistent behavior in your application.
Based on your testing through the SDK, REST API, and Azure Portal, this behavior is not expected. Device twin queries should return near-real-time data not versions that are several days old.
This issue occurs due to a known limitation in how IoT Hub handles device queries versus direct reads. Direct twin reads (GetTwinAsync) always fetch the latest live state from the service. However, query operations (/devices/query or GetNextAsTwinAsync) rely on an internal indexed store that is updated asynchronously. Normally, this index refreshes within seconds or minutes, but in your case the index appears stale for days, which is abnormal and indicates a backend issue.
The root cause is typically tied to indexing delays or failures within IoT Hub. The internal index may stop updating for some devices if the service is experiencing throttling, high ingestion rates, or a stuck update pipeline. In many cases, this issue occurs at the partition level IoT Hub stores devices across partitions, and if one partition's indexer fails, only those devices return stale data via queries.
Changing API versions (e.g., 2021-04-12 vs 2024-03-31) won’t resolve the issue because the underlying query-indexing mechanism is the same across all API versions.
To diagnose the issue, comparing the version from query results with direct reads as you’ve done is the correct first step.
If the difference is large (multiple days), this is not standard eventual consistency and clearly points to an indexing problem. You can also review IoT Hub metrics in Azure Monitor, such as TwinUpdateOperations, QueryOperations, throttling events, and errors.
Running queries for multiple devices that belong to the same partition can also confirm if the problem is isolated to a specific partition.
As a temporary workaround, avoid relying on queries for real-time or fast-changing twin data. Instead, use GetTwinAsync wherever accurate and latest state is required.
If you need to perform batch or large-scale processing, consider using device twin change notifications (Event Grid or routes) to maintain your own up-to-date cache, and reserve queries for static or slowly changing metadata such as tags or deviceId lists.
Please refer this
IoT Hub query language for device and module twins
Issues with twin reads or updates
I Hope this helps. Do let me know if you have any further queries.
Thank you!