23

As I have been developing app using Flutter. I came to find that app is heavy in size plus it is using a lot of space as app data and app cache. Is there any way to clear app cache programmatically?

Edit: my app's size in release mode is about 7mb and app data is about 11mb. My app opens one site within app and it also streams online radio so it's app data goes on increasing

DoeJ
  • 405
  • 1
  • 4
  • 14

5 Answers5

19

You can get the temp folder, and delete files in it.

var appDir = (await getTemporaryDirectory()).path;
new Directory(appDir).delete(recursive: true);
Vincent
  • 3,124
  • 3
  • 21
  • 40
  • 3
    Getting this error with this code, any ideas? Unhandled Exception: FileSystemException: Deletion failed, path = '/var/mobile/Containers/Data/Application/.../Library/Caches' (OS Error: Operation not permitted, errno = 1). - ran on iOS – Luke Stanyer May 17 '19 at 14:17
  • 2
    I didn't test on ios, cause I don't have a mac. – Vincent May 20 '19 at 07:48
  • i don't know and i was looking for the same solution as you but i think this is because of the permissions which can be solved by going to Android Manifest and Add file access permissions because right now your app doesn't have the permission to edit or delete files from the user's storage – RYOK Jun 29 '20 at 16:49
  • @RYOK, I have the same problem like this case, would you like to give example how to allow permission to edit or delete files from the user's storage ? – wahyu Sep 29 '20 at 07:06
  • @LukeStanyer did you find any solution of this " Unhandled Exception: FileSystemException: Deletion failed, path = '/var/mobile/Containers/Data/Application/.../Library/Caches' (OS Error: Operation not permitted, errno = 1)", I am having the same issue and couldn't find any solution. Thanks in advance. – Ashish Bharwal Feb 25 '21 at 07:07
  • @AshishBharwal This was a while ago now, but I think it was because I was trying to delete a directory that didn't exist. So I called it within an if dir.exists(); statement. – Luke Stanyer Feb 25 '21 at 10:52
9

You can also use use flutter_cache_manager to delete or remove your app's cache programatically.

After installing flutter_cache_manager , use this :

await DefaultCacheManager().emptyCache();
Shubhamhackz
  • 7,333
  • 7
  • 50
  • 71
6

it's working you just have to refresh your app after this.

import 'package:path_provider/path_provider.dart';

  Future<void> _deleteCacheDir() async {
    var tempDir = await getTemporaryDirectory();

    if (tempDir.existsSync()) {
      tempDir.deleteSync(recursive: true);
    }
  }

  Future<void> _deleteAppDir() async {
    var appDocDir = await getApplicationDocumentsDirectory();

    if (appDocDir.existsSync()) {
      appDocDir.deleteSync(recursive: true);
    }
  }
Community
  • 1
  • 1
aminbadri7
  • 89
  • 2
  • 9
3

Note on Vincent's answer.

Try to avoid clearing the device's temporary folder. It'll be saver to clear the app it's cache folder like:

var appDir = (await getTemporaryDirectory()).path + '/<package_name>';
new Directory(appDir).delete(recursive: true);
2

You Can Easily Remove cache.

import 'package:path_provider/path_provider.dart';
Future<void> _deleteCacheDir() async {
final cacheDir = await getTemporaryDirectory();
if (cacheDir.existsSync()) {
cacheDir.deleteSync(recursive: true);}}
final appDir = await getApplicationSupportDirectory();
if (appDir.existsSync()) {
appDir.deleteSync(recursive: true);}}
Hardas
  • 44
  • 3