Field notes
When an Entry Is Deleted in Apple Health: How an App Finds Out
An app finds out about a deleted Apple Health entry through an anchored query. Apple says the deletion records are temporary, so a separate log needs its own bookkeeping.
The short answer#
An app finds out that an Apple Health entry was deleted by asking HealthKit for changes since the last time it looked. The query that does this is an anchored object query, and it hands back deleted objects alongside new samples. The catch is that Apple documents those deletion records as temporary. If your log mirrors Apple Health, it needs its own bookkeeping for the gap.
How an anchored query reports deletions#
Apple describes an HKAnchoredObjectQuery as a query that returns changes to the HealthKit store, covering both a snapshot of what is there now and continuous monitoring. Its results handler receives three things: new samples, deleted objects, and a new anchor.
The anchor#
The anchor is a bookmark. Per Apple, the query returns an anchor value that corresponds to the last sample or deleted object it received, and a later query can pass that anchor back so it only gets newer saved or deleted objects. If you pass nil, you get a snapshot of everything currently matching, with no deletions to report because there is no earlier position to compare against.
The update handler#
If the query's updateHandler is set, the query keeps running. Apple's documentation for the update handler says the system calls it on a background queue whenever matching samples are saved to or deleted from the store. If the handler is nil, the query stops once it has the initial results.
What a deletion record contains#
The record is an HKDeletedObject, which Apple defines as an object that represents a sample that has been deleted from the HealthKit store. It carries two properties:
| Property | What Apple says it is |
|---|---|
| uuid | The identifier of the HealthKit object that was deleted |
| metadata | The metadata associated with the deleted object |
That's the whole list. The record identifies which entry went away. It is not a copy of the entry, so the value, the unit and the time are not part of what you are handed. This fits with how entries work in general: HealthKit samples are immutable, so a change is really a deletion plus a new sample with a new identifier.
For a log, the consequence is practical. To act on a deletion, you must already have stored that uuid next to your own copy of the entry. Without it, a deletion notice has nothing to point at.
Deletion records are temporary#
Apple's wording is direct: "Deleted objects are temporary; the system may remove them from the HealthKit store at any time to free up space." Apple does not state a retention period in the page I read, so there is no number to plan around. A record might be there for a long time or might be gone soon.
What follows from that:
- An app that checks often is more likely to see a deletion than one that checks rarely.
- An app that was not opened for a while may miss a deletion entirely, with nothing left to say the entry ever existed.
- Absence of a deletion record does not prove the entry is still there.
That last point matters most: the record is a notice, not a ledger.
Observer queries and background delivery#
Apple's suggested route for reliable notification is to create an HKObserverQuery and register it for background delivery. The system then wakes the app and calls the observer's update handler when matching objects change, deletions included. The observer does not say which objects were deleted. Inside its handler, the app runs an anchored object query to find out.
Even this is a best-effort path in the sense Apple describes, since the deletion records the follow-up query depends on can be removed. It lowers the chance of missing a deletion. It does not make the chance zero.
Reconciling by identifier#
A deletion notice and a full comparison answer different questions, and you want both. A notice says "this identifier was removed since your anchor." A comparison says "here is what exists now, and here is what I have." The second is the only way to catch a deletion whose record has already expired.
Running an anchored object query with a nil anchor gives Apple's snapshot of what is currently stored. Compare the identifiers in that snapshot with the uuid values you saved. Any saved identifier missing from the snapshot was removed at some point, even if you were never told. You cannot learn when it was removed, or by whom, only that it is gone. Write that down as what it is: an entry that disappeared upstream between two dates you can name.
Both methods have a role. Use the anchor for the routine, cheap check. Use the snapshot comparison now and then as a backstop, and after any long stretch when the app was not running.
Who gets told#
HealthKit access is granted per data type, and an app only sees what you allowed it to read. If you want to check what each app can see and change, see how to review Apple Health app permissions. An app without read access to a type never receives deletion records for it.
What to record in your own log#
If your log mirrors entries from Apple Health, a few habits keep it honest when something is deleted upstream:
- Store the source
uuidwith every mirrored entry. - Store the last anchor you processed, so each check starts where the previous one ended.
- When a deletion record arrives, mark your copy as removed upstream with the date you learned of it, rather than erasing it. A deleted entry that you silently drop looks identical to one that never existed.
- Note a long gap between checks as a known blind spot. After a gap, an entry that has quietly disappeared upstream may still show as present in your copy.
- Keep a periodic raw copy of your Apple Health export, so you have an independent record to compare against.
The point of a log you audit later is that it does not depend on a notice the system is allowed to discard. An app like OptimizedTracker, which keeps its own entries, is a place where that bookkeeping can live.
Share
Related
01
