4

Here I want to clear my app data on a button click.It needs to clear the whole app data and cache memory of the app.

Hope you understand the question. Thanks.

I have some suggestions about it, but I'm not sure if this works. Here is some code:

var appDir = (await getTemporaryDirectory()).path;
new Directory(appDir).delete(recursive: true);
jatin gajera
  • 61
  • 1
  • 4
  • 1
    I have the same problem with you, have you found the solution? – wahyu Sep 29 '20 at 07:26
  • 2
    You might find your answer [here](https://stackoverflow.com/questions/59232893/how-to-delete-cache-and-app-dir-in-flutter), using the path provider. – harlandgomez Feb 07 '21 at 20:00
  • Does this answer your question? [How to Delete cache and app dir in flutter](https://stackoverflow.com/questions/59232893/how-to-delete-cache-and-app-dir-in-flutter) –  Mar 12 '21 at 08:04

1 Answers1

0

The following line you've provided is trying to clear the device's temporary folder.

var appDir = (await getTemporaryDirectory()).path;
new Directory(appDir).delete(recursive: true);

But sometimes avoiding to clear the temp folder will do better and clearing the app's cache folder instead. Just like this:

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

Where package_name refers to the app's Bundle ID.

There are also some packages that you could use like the path_provider package as mentioned in the comments section. It allows us to access commonly used locations on the device’s filesystem. You can do something like this:

Future<void> _deleteCacheDir() async {
    final cacheDir = await getTemporaryDirectory();

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

  Future<void> _deleteAppDir() async {
    final appDir = await getApplicationSupportDirectory();

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

Here is a good reference for path_provider.

MαπμQμαπkγVπ.0
  • 5,887
  • 1
  • 27
  • 65