flutter_cache_manager icon indicating copy to clipboard operation
flutter_cache_manager copied to clipboard

emptyCache() does not work anymore

Open desmeit opened this issue 6 months ago • 3 comments

I use 3.4.1.

I work with

await cacheManager.downloadFile(url);

and after that I get it from the cache with:

 final fileInfo = await cacheManager.getFileFromCache(
          url,
        );

I try to delete the cache with

await cacheManager.emptyCache();

It was working for a long time but now it is not working anymore.

flutter 3.29.3

desmeit avatar Jul 10 '25 10:07 desmeit

I have got same issue void _clearCache() async{ DefaultCacheManager().emptyCache(); setState(() { fileStream = null; }); } It is not remove cache

hafizramiz avatar Sep 07 '25 10:09 hafizramiz

(running Flutter 3.32.8, flutter_cache_manager: ^3.4.1 on a MacOS) Just a quick check it seems like:

cache_store.dart > class CacheStore _Future removeCachedFile successfully remove files from the _memCache

though the absolute path of the file is wrong showing this: "/Users//Library/Containers//Data/filename.jpg" instead of "/Users//Library/Containers//Data/Library/Caches//filename.jpg"

So line 187 final file = io.File(cacheObject.relativePath); points to a non existing file and line 189 if (file.existsSync()) always fails.

This creates a secondary defect as now files will be re-downloaded and duplicated in disk.

For a potential fix, you might want to look into this PR: https://github.com/Baseflow/flutter_cache_manager/pull/477/commits/3d1995f175087475e6c2d8f78fd59a0683e773f7

colnaghijr avatar Sep 09 '25 17:09 colnaghijr

@hafizramiz

I have temporarily solved it manually until the plugin developer addresses the problem:

  Future<void> clearCachePhysically() async {
    final baseDir = await getTemporaryDirectory();
    final cacheDir = Directory('${baseDir.path}/libCachedImageData');
    if (await cacheDir.exists()) {
      await cacheDir.delete(recursive: true);
      print("Cache deleted.");
    }
  }

desmeit avatar Sep 10 '25 09:09 desmeit