clearTempDir static method

Future<void> clearTempDir()

Clears all files and directories from the temporary directory.

This method is only available on non-web platforms. On web, it does nothing. All files and subdirectories in the temporary directory are deleted recursively. Errors during deletion are caught and ignored to ensure the process continues even if some files cannot be deleted.

Debug prints are used to log the deletion process, showing each file being deleted and the total count of deleted items.

Implementation

static Future<void> clearTempDir() async {
  if (!kIsWeb) {
    final tempDir = await getTemporaryDirectory();
    final files = tempDir.listSync();
    int i = 0;
    for (final file in files) {
      try {
        debugPrint("Deleting ${file.path}");
        if (file is File || file is Directory) {
          await file.delete(recursive: true);
        }
        i++;
      } catch (_) {
        // Ignore errors during deletion
      }
    }
    debugPrint("Deleted $i files");
  }
}