5

How can I remove cookies, local storage and other crap from the "AppData\roaming\MyApp" folder, when the electron application quits?

I tried deleting the whole directory on app quit, but it throws me EBUSY errors. Apparently the files are locked or something, almost like someone doesn't want us to be able to remove the bloat?

const fs = require('fs-extra');

const clearBloat = async () => fs.remove(path.join(app.getPath('appData'), app.name));

app.on('window-all-closed', async () => {
  await clearBloat();
});
Alex
  • 66,732
  • 177
  • 439
  • 641
  • Suggestion: `app.setPath("userData", app.getPath("temp") + "/" + app.name)` by storing user data in the `temp` directory, the OS will take care of cleanup. – aabuhijleh Sep 10 '20 at 18:36
  • You will probably have to spawn a different process before exit to do that work for you because Chromium will be using some of the files before exiting. Not sure about the implementation of Electron though which might have modified or handled that behavior – Asesh Sep 14 '20 at 02:55
  • 1
    https://stackoverflow.com/questions/55081270/getting-error-ebusy-resource-busy-or-locked Please refer to this link there are many solutions. – Prakash Harvani Sep 14 '20 at 04:14
  • Your current code will be working well on MacOS – tpikachu Sep 14 '20 at 14:47
  • Have you tried this API: https://www.electronjs.org/docs/api/session#sesclearstoragedataoptions ? – Asesh Sep 16 '20 at 12:54
  • @Asesh I've personally tried but can't get the promise to resolve on app quit. Looking for help if you've gotten farther. – reZach Sep 17 '20 at 01:42
  • @Alex - Please review my latest update, I got it working. – reZach Sep 17 '20 at 04:39
  • @Asesh: yes, and it doesn't clear these folders – Alex Sep 21 '20 at 13:43

1 Answers1

3

After doing some testing, I've found that you have to delete the files after your electron process has ended (trying to delete the files while in the quit or will-quit app events doesn't delete the files/folders; they get re-created right away. Something in electron (likely, Chromium) wants these files/folders to exist while the app is running, and it's too much work to figure out how to hook into it).

What works for me is spawning a detached cmd off a shell that waits 3 seconds, and then deletes all files/folders in a given application folder. What will be an exercise up to the reader will be to hide the output of the ping command (or hide the window, but there's been mixed success on that front), or choose a different command. I've found timeout works, but sleep and choice (ie. something like this) do not work.

Here's what you will need to add:

const { app } = require("electron");
const { spawn } = require("child_process");
const path = require("path");

...

app.on("will-quit", async (event) => {
  const folder = path.join(app.getPath("appData"), app.name);

  // Wait 3 seconds, navigate into your app folder and delete all files/folders
  const cmd = `ping localhost -n 3 > nul 2>&1 && pushd "${folder}" && (rd /s /q "${folder}" 2>nul & popd)`;
  
  // shell = true prevents EONENT errors
  // stdio = ignore allows the pipes to continue processing w/o handling command output
  // detached = true allows the command to run once your app is [completely] shut down
  const process = spawn(cmd, { shell: true, stdio: "ignore", detached: true });

  // Prevents the parent process from waiting for the child (this) process to finish
  process.unref();
});

As another user mentioned, there's a method available on your electron session that is a native API that clears all of these files/folders. However, it returns a promise, and I could not figure out how to execute this synchronously within one of these app-events.

Reference #1

reZach
  • 8,945
  • 12
  • 51
  • 97
  • 1
    spawn your process with `detached: true` and unref it to prevent electron killing it, and delay the removal in your process for 1-2s – nice ass Sep 16 '20 at 05:21
  • Thanks @niceass, can you describe what you mean by "unref it"? **Edit** I see now, I will update my answer, doesn't complete get what the OP was looking for, but gets us closer. – reZach Sep 16 '20 at 05:27
  • 1
    More on `Something in electron wants these files/folders to exist while the app is running`: Electron uses Chromium, so basically Chromium will be using those files – Asesh Sep 17 '20 at 16:34
  • Thanks @Asesh, I've added a note to my answer to reflect this – reZach Sep 17 '20 at 17:07