Reputation: 465
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
Upvotes: 29
Views: 49774
Reputation: 129
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);
}
}
Upvotes: 9
Reputation: 34
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);}}
Upvotes: 2
Reputation: 7963
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();
Upvotes: 13
Reputation: 128
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);
Upvotes: 3
Reputation: 3294
You can get the temp folder, and delete files in it.
var appDir = (await getTemporaryDirectory()).path;
new Directory(appDir).delete(recursive: true);
Upvotes: 23