清除 Electron 中的快取資料

龙陌發表於2024-08-19

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);
  }
}

相關文章