I am using a flutter plugin named path_provider. I have to store image file at path_provider.getTemporaryDirectory(). Is the image stored here is deleted automatically or I have to do it explicitly.

Solution 1: Sami Kanafani

from the documentation of path_provider

Files in this directory may be cleared at any time. This does not return a new temporary directory. Instead, the caller is responsible for creating (and cleaning up) files or directories within this directory. This directory is scoped to the calling application.

So you are responsible for cleaning up, which means it is not automatically cleared, but it may be cleared any time


You can clear the temporary the directory as follows:

import 'dart:io';
Directory dir = await getTemporaryDirectory();
dir.deleteSync(recursive: true);
dir.create(); // This will create the temporary directory again. So temporary files will only be deleted

Solution 2: fabienbranchel

To keep things safe, I store all the file paths in list with flutter_secure_storage, then when I launch the app I browse all the file paths, check if file still exists (can be previously deleted by system) and delete it. Finally, I clear the list from flutter_secure_storage for next time.