cleanupOrphans method

Future<List<OpResult>> cleanupOrphans()

Removes self-owned items whose parent chain has been broken. Call this at startup to reclaim storage from orphaned sub-items after an offline period during which a parent may have been deleted on another atSign.

Behaviour depends on the collection instance:

  • Sub-collection (instance returned by subCollection): if its bound parent no longer exists locally, delete every self-owned item that matches this sub-collection's ancestor- owner chain. Cross-owner same-id chains are spared.

  • Root / standalone collection: scan every self-owned descendant (any sub-collection, any depth) under this collection's namespace; for each, walk the full ancestor chain (ids from the key + owners from the envelope parents field) and delete if any level is missing locally. Legacy items with no parents envelope fall back to a root-ancestor-only check.

The cleanest place to invoke this is implicitly via AtClient.collection's cleanupOrphansOnCreation: true flag — the library then runs one sweep before the returned Future completes. Direct invocation is also supported for apps that want to sweep at other moments.

Returns per-key OpResults for every deletion attempted; on a sub-collection whose parent still exists, returns an empty list (no-op).

Implementation

Future<List<OpResult>> cleanupOrphans() async {
  return isSubCollection
      ? _cleanupOrphansFromSub()
      : _cleanupOrphansFromRoot();
}