清除 Electron 中的缓存数据

Electron 将其缓存存储在以下文件夹中:

window :
C:\Users<user>\AppData\Roaming<yourAppName>\Cache

Linux:
/home//.config//Cache

操作系统:
/Users//Library/Application Support//Cache

let cache = app.getPath('cache');
// 获取缓存的路径
const cachePath = path.join(cache, 'appName');
 // 清理缓存目录下的文件
if (fs.existsSync(cachePath)) {
   var deletePath = ['blob_storage', 'Code Cache'];
   for (var i = 0; i < deletePath.length; i++) {
       deleteDirectoryRecursive(path.join(cachePath, deletePath[i]));
   }
 }

function deleteDirectoryRecursive(directoryPath) {
  if (fs.existsSync(directoryPath)) {
    fs.readdirSync(directoryPath).forEach(function(file, index) {
      var curPath = path.join(directoryPath, file);
      if (fs.lstatSync(curPath).isDirectory()) { // recurse
        deleteDirectoryRecursive(curPath);
      } else { // delete file
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(directoryPath);
  }
}
posted @ 2024-08-19 15:04  龙陌  阅读(1908)  评论(1)    收藏  举报