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
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'
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._logger.info(`cache clean: ${dirPath} is deleted!`);
the cache directory was not deleted. 5 files were still inside it.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
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/
If you want to clear any remnants of previous login sessions, you'd better use this:
loginWindow.webContents.session.clearStorageData()
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();
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 ;-)