Apollo Kotlin Normalized Cache Help

Garbage collection

The garbage collection feature allows to remove unused data from the cache to reduce its size.

Stale fields

A field is considered stale if its received date is older than its (client controlled) max age, or if its (server controlled) expiration date has passed.

See Expiration for more information about staleness.

Stale fields can be removed from the cache by calling the ApolloStore.removeStaleFields() function.

If all fields of a record are stale, the record itself is removed.

Note: when a record is removed, any reference to it will become a dangling reference.

Dangling references

A dangling reference is a field whose value is a reference to a cache key that does not exist in the cache.

This can happen when:

Dangling references can be removed from the cache by calling the ApolloStore.removeDanglingReferences() function.

If all fields of a record are dangling references, the record itself is removed.

Note: if a field's value is a list for which one or more elements are dangling references, the entire list is removed, which can result in unreachable records.

Unreachable records

A record is unreachable if there exists no chain of references from the root record to it.

This can happen when:

  • data that was previously returned by the server is no longer returned by a subsequent query

  • manually adding records with ApolloStore.writeFragment()

  • manually deleting records with ApolloStore.remove() and cascade = false: the deleted record could be the only one referencing others

  • references get deleted because they're stale

Unreachable records can be removed from the cache by calling the ApolloStore.removeUnreachableRecords() function.

ApolloStore.garbageCollect()

The ApolloStore.garbageCollect() function is a convenience to remove all stale fields, dangling references, and unreachable records from the cache.

Last modified: 06 August 2025