From 2920f77c7529ff23da1861edcb9e962c6b9daee2 Mon Sep 17 00:00:00 2001 From: ashilkn Date: Sat, 12 Jul 2025 11:21:35 +0530 Subject: [PATCH 1/2] If too many local files are attempted to be deleted using free up space feature, reduce the batch size and try deleting --- .../photos/lib/utils/delete_file_util.dart | 62 +++++++++++++++++-- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/mobile/apps/photos/lib/utils/delete_file_util.dart b/mobile/apps/photos/lib/utils/delete_file_util.dart index 11a425afa0..839ba7712d 100644 --- a/mobile/apps/photos/lib/utils/delete_file_util.dart +++ b/mobile/apps/photos/lib/utils/delete_file_util.dart @@ -339,6 +339,9 @@ Future deleteLocalFiles( final List deletedIDs = []; final List localAssetIDs = []; final List localSharedMediaIDs = []; + + const largeCountThreshold = 20000; + final tooManyAssets = localIDs.length > largeCountThreshold; try { for (String id in localIDs) { if (id.startsWith(sharedMediaIdentifier)) { @@ -350,11 +353,23 @@ Future deleteLocalFiles( deletedIDs.addAll(await _tryDeleteSharedMediaFiles(localSharedMediaIDs)); final bool shouldDeleteInBatches = - await isAndroidSDKVersionLowerThan(android11SDKINT); + await isAndroidSDKVersionLowerThan(android11SDKINT) || tooManyAssets; if (shouldDeleteInBatches) { - _logger.info("Deleting in batches"); - deletedIDs - .addAll(await deleteLocalFilesInBatches(context, localAssetIDs)); + if (tooManyAssets) { + _logger.info( + "Too many assets (${localIDs.length}) to delete in one shot, deleting in batches", + ); + await _recursivelyReduceBatchSizeAndRetryDeletion( + batchSize: largeCountThreshold, + context: context, + localIDs: localIDs, + deletedIDs: deletedIDs, + ); + } else { + _logger.info("Deleting in batches"); + deletedIDs + .addAll(await deleteLocalFilesInBatches(context, localAssetIDs)); + } } else { _logger.info("Deleting in one shot"); deletedIDs @@ -625,6 +640,45 @@ Future> deleteLocalFilesInBatches( return deletedIDs; } +Future _recursivelyReduceBatchSizeAndRetryDeletion({ + required int batchSize, + required BuildContext context, + required List localIDs, + required List deletedIDs, + int minimumBatchSizeThresholdToStopRetry = 2000, +}) async { + if (batchSize < minimumBatchSizeThresholdToStopRetry) { + _logger.warning( + "Batch size is too small ($batchSize), stopping further retries.", + ); + throw Exception( + "Batch size reached minimum threshold: $batchSize. Stopping further retries.", + ); + } + try { + deletedIDs.addAll( + await deleteLocalFilesInBatches( + context, + localIDs, + minimumBatchSize: 1, + maximumBatchSize: batchSize, + minimumParts: 1, + ), + ); + } catch (e) { + _logger.warning( + "Failed to delete local files in batches of $batchSize. Reducing batch size and retrying.", + e, + ); + await _recursivelyReduceBatchSizeAndRetryDeletion( + batchSize: (batchSize / 2).floor(), + context: context, + localIDs: localIDs, + deletedIDs: deletedIDs, + ); + } +} + Future _localFileExist(EnteFile file) { if (file.isSharedMediaToAppSandbox) { final localFile = File(getSharedMediaFilePath(file)); From e259b06d636de44992adf356102ec586752a9f7c Mon Sep 17 00:00:00 2001 From: ashilkn Date: Sat, 12 Jul 2025 11:32:44 +0530 Subject: [PATCH 2/2] Minor correction in execption --- mobile/apps/photos/lib/utils/delete_file_util.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mobile/apps/photos/lib/utils/delete_file_util.dart b/mobile/apps/photos/lib/utils/delete_file_util.dart index 839ba7712d..efb9db6093 100644 --- a/mobile/apps/photos/lib/utils/delete_file_util.dart +++ b/mobile/apps/photos/lib/utils/delete_file_util.dart @@ -652,7 +652,7 @@ Future _recursivelyReduceBatchSizeAndRetryDeletion({ "Batch size is too small ($batchSize), stopping further retries.", ); throw Exception( - "Batch size reached minimum threshold: $batchSize. Stopping further retries.", + "Batch size is too small ($batchSize), stopping further retries.", ); } try {