cleanupOrphanedFiles static method

Future<int> cleanupOrphanedFiles({
  1. required List<String> protectedFiles,
  2. List<String>? supportedExtensions,
  3. bool enableResumeDetection = true,
})

Cleans up orphaned files

⚠️ USER MUST CALL THIS EXPLICITLY - it is NOT called automatically!

This method deletes files that don't have active download tasks. Use getOrphanedFiles() first to see what will be deleted.

Returns the number of files deleted.

Implementation

static Future<int> cleanupOrphanedFiles({
  required List<String> protectedFiles,
  List<String>? supportedExtensions,
  bool enableResumeDetection = true,
}) async {
  debugPrint('⚠️  cleanupOrphanedFiles() called explicitly by user');

  final orphaned = await getOrphanedFiles(
    protectedFiles: protectedFiles,
    supportedExtensions: supportedExtensions,
  );

  int deletedCount = 0;
  for (final info in orphaned) {
    try {
      await File(info.path).delete();
      deletedCount++;
      debugPrint('Deleted orphaned file: ${info.filename}');
    } catch (e) {
      debugPrint('Failed to delete ${info.filename}: $e');
    }
  }

  debugPrint('Cleaned up $deletedCount orphaned files');
  return deletedCount;
}