25

In my flutter app, I store some images in cache directory and some files in application document directory, now I want to add possibility to my users to delete the cache dir and app dir, How can I achieve this?

mohammad
  • 2,568
  • 3
  • 20
  • 41

2 Answers2

34

You need path_provider package

Then try this code:

  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);
    }
  }
Ramin
  • 459
  • 5
  • 10
  • 1
    Thanks, it helps, also do you know how can I read the amount of cache and file capacity which it occupied before delete process – mohammad Dec 08 '19 at 06:38
  • 1
    Salam Ramin, it helps a lot. – mohammad Dec 08 '19 at 06:55
  • @Ramin Hi, I try your code, it works... but when I check my apps size (data and cache) in my hone setting, there are still data and cache... they didn't reduce the data and cache to 0 bytes... am I doing wrong? – wahyu Sep 29 '20 at 07:15
  • @uyhaW Hi, I just checked that, and even if u clear cache & data using phone settings then reopen the app info it doesn't show 0 bytes. So I guess its normal. – Ramin Sep 29 '20 at 08:58
  • where can I use this code? main? – Kamrujjaman Joy Sep 25 '22 at 12:17
  • Thanks for your time. But it doesn't making the data empty. – Balaji Ks May 22 '23 at 10:11
-1

This code may help you :

import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

void main() {
  runApp(
    MaterialApp(
      title: 'Reading and Writing Files',
      home: FlutterDemo(storage: CounterStorage()),
    ),
  );
}

class CounterStorage {
  Future<String> get _localPath async {
    final directory = await getApplicationDocumentsDirectory();

    return directory.path;
  }

  Future<File> get _localFile async {
    final path = await _localPath;
    return File('$path/counter.txt');
  }

  Future<int> readCounter() async {
    try {
      final file = await _localFile;

      // Read the file
      String contents = await file.readAsString();

      return int.parse(contents);
    } catch (e) {
      // If encountering an error, return 0
      return 0;
    }
  }

  Future<File> writeCounter(int counter) async {
    final file = await _localFile;

    // Write the file
    return file.writeAsString('$counter');
  }
}

class FlutterDemo extends StatefulWidget {
  final CounterStorage storage;

  FlutterDemo({Key key, @required this.storage}) : super(key: key);

  @override
  _FlutterDemoState createState() => _FlutterDemoState();
}

class _FlutterDemoState extends State<FlutterDemo> {
  int _counter;

  @override
  void initState() {
    super.initState();
    widget.storage.readCounter().then((int value) {
      setState(() {
        _counter = value;
      });
    });
  }

  Future<File> _incrementCounter() {
    setState(() {
      _counter++;
    });

    // Write the variable as a string to the file.
    return widget.storage.writeCounter(_counter);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Reading and Writing Files')),
      body: Center(
        child: Text(
          'Button tapped $_counter time${_counter == 1 ? '' : 's'}.',
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Also, you can find very helpful document here: https://github.com/flutter/samples/blob/master/INDEX.md

XAMT
  • 1,515
  • 2
  • 11
  • 31
  • Hi XAMT, it is not the solution, what I want is to clear files, and images because of cacheImages and other files which are in cache and app dir, so I want to clear them inside app by users to increase the performance of app – mohammad Dec 08 '19 at 06:34
  • 1
    import 'dart:io'; void main() { final dir = Directory(dirPath); dir.deleteSync(recursive: true); } – XAMT Dec 08 '19 at 06:40