From cdcbfbf108f639ea2f33c4b78815647c265438ae Mon Sep 17 00:00:00 2001 From: Developer Date: Thu, 3 Sep 2026 09:16:14 -0400 Subject: [PATCH] fix: recursively clean up MotorCloud account data --- .../cloud/account_cloud_cleanup_service.dart | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/services/cloud/account_cloud_cleanup_service.dart b/lib/services/cloud/account_cloud_cleanup_service.dart index 291ad12..c603d64 100644 --- a/lib/services/cloud/account_cloud_cleanup_service.dart +++ b/lib/services/cloud/account_cloud_cleanup_service.dart @@ -49,15 +49,31 @@ class AccountCloudCleanupService implements CloudAccountCleanup { if (firestore == null) throw StateError('MotorCloud database is unavailable.'); final userRef = firestore.collection('users').doc(user.uid); final rides = await userRef.collection('rides').get(); - final motorcycles = await userRef.collection('motorcycles').get(); - final batch = firestore.batch(); for (final doc in rides.docs) { - batch.delete(doc.reference); + await _deleteCollection(doc.reference.collection('events')); + await _deleteCollection(doc.reference.collection('performance')); } - for (final doc in motorcycles.docs) { - batch.delete(doc.reference); + + await _deleteCollection(userRef.collection('rides')); + await _deleteCollection(userRef.collection('motorcycles')); + await _deleteCollection(userRef.collection('trips')); + await _deleteCollection(userRef.collection('events')); + await _deleteCollection(userRef.collection('friends')); + + // Delete the profile last so retries can still discover the account scope. + final profileBatch = firestore.batch()..delete(userRef); + await profileBatch.commit(); + } + + Future _deleteCollection(CollectionReference> collection) async { + final snapshot = await collection.get(); + for (var offset = 0; offset < snapshot.docs.length; offset += 450) { + final batch = _firestore!.batch(); + final end = (offset + 450).clamp(0, snapshot.docs.length); + for (final doc in snapshot.docs.sublist(offset, end)) { + batch.delete(doc.reference); + } + await batch.commit(); } - batch.delete(userRef); - await batch.commit(); } }