5

I recently discovered that c:/Users/username/AppData/Local/AppName/Cache contained over 100K cache files.

I read up a bit and saw that the gui.App.clearCache() method should fix this. I execute this method at start and at shutdown of the app. (after assigning gui to require("nw.gui"))

However, this doesn't seem to help. Nothing is being deleted.

Any ideas why that is?

r0-
  • 2,388
  • 1
  • 24
  • 29

3 Answers3

3

The require cache is apparently only available on global.require.cache.

Clearing that cache manually made it work for me.

for(module in global.require.cache){
    if(global.require.cache.hasOwnProperty(module)){
        delete global.require.cache[module];
    }
}
location.reload()

Neither the gui.APP.clearCache() nor the gui.Window.get().reloadIgnoringCache() had any effect for me.

asbjornenge
  • 1,209
  • 13
  • 16
  • 1
    This does not work for me. Because i have to reload a page from https://www.google.com and make it cleared cache. –  Nov 13 '14 at 21:54
  • gui.APP.clearCache() is working for me. Potentially this is due to an update to NW. I am running 0.11.5. Also this answer appears to no longer work. – Scott Wright Jan 08 '15 at 18:25
  • Addition to above code: If it's for (var module...) it works in Strict mode too. – Philipp Lenssen Oct 15 '15 at 06:59
0

Ran into the same issue so I created a quick solution to solve the problem temporarily until I can rely on the native gui.App.clearCache function. This removes the entire Cache directory and recreates it asynchronously.

This requires fs-extra and should work on Linux, OSX and Windows.

var customClearCache = function(){
  var dir = path.join(gui.App.dataPath, '/Cache');
  fs.remove(dir, function(err) {
    if (err) return console.error(err)
    fs.mkdirs(dir, function(err) {
      if (err) return console.error(err)
      // This is where I start my app
    });
  });
};
Josh Lankford
  • 468
  • 2
  • 5
0

It is now 2019 and clearCache() does function by adding the following:

var gui = require('nw.gui'); 
gui.App.clearCache();

or

nw.App.clearCache();

This will clear the HTTP cache in memory and the one on disk whatever your platform. I have tested it on Mac OS but maybe there can be user permission issues on Linux.

On MacOS, you can check in real time that the cache folder is emptied at ‎⁨

Mac HD⁩ ▸ ⁨Users⁩ ▸ youruser ▸ ⁨Library⁩ ▸ ⁨Caches⁩ ▸ ⁨yourapp ▸ ⁨Default⁩

Nicolas Guérinet
  • 2,086
  • 1
  • 29
  • 38