54

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 any api or any other way please let me know. comments are appreciated .

neel
  • 779
  • 1
  • 6
  • 11

8 Answers8

97

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

pagep
  • 3,488
  • 1
  • 26
  • 47
  • 2
    on Windows you get cache path by `const process = require('process');` `process.env.APPDATA + "\\"+ app.getName() + "\\Cache";` I am not sure will it work on other platforms. – Nuryagdy Mustapayev Dec 08 '20 at 15:38
38

You can use session.clearCache api.

var remote = require('remote'); 
var win = remote.getCurrentWindow();
win.webContents.session.clearCache(function(){
//some callback.
});
Praveen kumar
  • 381
  • 3
  • 4
15

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

loginWindow.webContents.session.clearStorageData()
goodhyun
  • 4,814
  • 3
  • 33
  • 25
9

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();
thegnuu
  • 697
  • 6
  • 9
2

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/

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
neel
  • 779
  • 1
  • 6
  • 11
2

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

Nuryagdy Mustapayev
  • 667
  • 1
  • 7
  • 27
0

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'

undefinederror
  • 821
  • 1
  • 8
  • 16
0

when you are developing, in developer tools go to the tab application and in clear storage and clear site data

  • 1
    How does this help in an electron application? – Derek Pollard Apr 08 '19 at 15:36
  • CTRL+SHIFT+I opens up the dev tools in Electron apps. This answer is one of possible solutions. – Neoraptor Sep 29 '19 at 11:27
  • Thank you, this is helpful if you just need to quickly clear the cache without writing code for it for a one time thing. The original question wasn't clear that it needed a code solution or a 1 time solution. – Trevor Nov 26 '19 at 17:02