clearCache static method
Deletes cache files and returns deleted count.
Implementation
static Future<int> clearCache({String? cacheSubdirectory, Duration? olderThan}) async {
try {
final cacheDirectory = await _getCacheDirectory(cacheSubdirectory, createIfMissing: false);
if (cacheDirectory == null) {
return 0;
}
int deletedCount = 0;
final now = DateTime.now();
await for (final entity in cacheDirectory.list()) {
if (entity is! File) {
continue;
}
var shouldDelete = true;
if (olderThan != null) {
final stat = await entity.stat();
shouldDelete = now.difference(stat.modified) > olderThan;
}
if (!shouldDelete) {
continue;
}
try {
await entity.delete();
deletedCount++;
} catch (e) {
debugLog('Error deleting cached file ${entity.path}: $e');
}
}
return deletedCount;
} catch (e) {
debugLog('Error clearing cache: $e');
return 0;
}
}