cleanupOldMessageFiles function

Future<CleanupResult> cleanupOldMessageFiles()

Clean up old message and error log files.

Implementation

Future<CleanupResult> cleanupOldMessageFiles() async {
  final cutoffDate = getCutoffDate();
  final errorPath = _getErrorsDir();
  final baseCachePath = _getBaseLogsDir();

  var result = await _cleanupOldFilesInDirectory(
    errorPath,
    cutoffDate,
    isMessagePath: false,
  );

  // Clean up MCP logs.
  try {
    final baseDir = Directory(baseCachePath);
    if (!await baseDir.exists()) return result;

    await for (final entity in baseDir.list()) {
      if (entity is Directory &&
          p.basename(entity.path).startsWith('mcp-logs-')) {
        result =
            result +
            await _cleanupOldFilesInDirectory(
              entity.path,
              cutoffDate,
              isMessagePath: true,
            );
        await _tryRmdir(entity.path);
      }
    }
  } on PathNotFoundException {
    // Ignore.
  } catch (_) {}

  return result;
}