How to clear the cache data in Electron(atom shell)?

后端 未结 8 839
有刺的猬
有刺的猬 2020-12-13 18:48

I want to clear cache data in Electron(atom-shell). I don\'t find any api like gui.App.clearCache()(node-webkit api to clear cache data) in Electron. If you find an

相关标签:
8条回答
  • 2020-12-13 18:58

    you could try mainWindow.webContents.clearHistory(); or deleting contents in the app Cache folders (will be recreated on app run). You can get the path with app.getPath('userData') + '/Cache'

    0 讨论(0)
  • 2020-12-13 19:01

    Tried answer from @thegnuu and session.defaultSession.clearCache(); on Windows, electron v10.1.5.

    Option 1: Deleting cache path, C:\Users\<username>\AppData\Roaming\<appname>\Cache, directly:

    _deleteFolder(dirPath) {
            const fs = require('fs');
    
            // delete directory recursively
            try {
                fs.rmdirSync(dirPath, {recursive: true});
                this._logger.info(`cache clean: ${dirPath} is deleted!`);
            } catch (e) {
                this._logger.error(`cache clean: could not delete  ${dirPath}!`, e);
            }
        }
    

    Option 2: which also clears the same C:\Users\<username>\AppData\Roaming\<appname>\Cache directory

    const {session}   = require('electron');
    session.defaultSession.clearCache();
    

    Problem with option 1:

    • This method sometimes caused app to abrupt crash (in this case, catch block did not run).
    • Also, after app restart, app could not load http assets. Normally after cache folder cleared, when I start app I can see new cache files are created. However, on option 1, app could not load assets, even after second restart. Deleted Cache directory manually, then app started work normally.
    • Although I did get log from this._logger.info(`cache clean: ${dirPath} is deleted!`); the cache directory was not deleted. 5 files were still inside it.
    • Tried to use asynchronic fs.rmdir, got the same result.

    On option 2 I did not face any problems. I guess it is the best option.

    Bonus: session.defaultSession.clearStorageData(); clears C:\Users\<username>\AppData\Roaming\<app name>\Local Storage directory

    0 讨论(0)
  • 2020-12-13 19:04

    Answer:

    var remote = require('remote');
    var win = remote.getCurrentWindow();
    
    win.WebContents.session.cookies.get(details, callback) // getting cookies
    win.WebContents.session.cookies.remove(details, callback) //deleting cookies
    

    For more info: http://electron.atom.io/docs/v0.29.0/api/browser-window/

    0 讨论(0)
  • 2020-12-13 19:07

    If you want to clear any remnants of previous login sessions, you'd better use this:

    loginWindow.webContents.session.clearStorageData()
    
    0 讨论(0)
  • 2020-12-13 19:07

    We are using this in our app...

    const { app, session } = require('electron');
    
    // ...
    
    session.defaultSession.clearStorageData(null, (error: any) => {
      // in our case we need to restart the application
      // app.relaunch();
      // app.exit();
    });
    

    Update for Electron 7:

    await session.defaultSession.clearStorageData();
    
    0 讨论(0)
  • 2020-12-13 19:10

    The Electron stores it's cache in these folders:

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

    Linux:
    /home/<user>/.config/<yourAppName>/Cache

    OS X:
    /Users/<user>/Library/Application Support/<yourAppName>/Cache

    So deleting these folders can also help you. Of course this is one time solution ;-)

    0 讨论(0)
提交回复
热议问题