Deleting temporary files on workspace close or exit


my application is creating some temporary files while the user is using it. The files are related to the workspace (or rather, files in the workspace) that the user currently has open.

In order to not waste disk space, I’d like to delete those temporary files either when the workspace is closed (possibly right before or right after it’s closed) or, if that’s not possible, when the application itself is being closed.

What would be the best way to do this? I was looking for some event that I could subscribe to which would notify me that the workspace is about to get closed or has been closed, but I can’t find anything like that?

Thank you!

@mnaglic I believe you can use the FrontendApplicationStateService to listen to the onStateChanged event (for something like closing_window) for you to perform your cleanup.

Possible options are the following:

I’ll check it out, thanks!

@vince-fugnitto Thanks for the advice! It didn’t work for me, unfortunately, but I did eventually manage to find a way to do the cleanup so I’ll write down my findings below for anybody who stumbles upon the same problem in the future.

At the moment of writing this, there’s no way to get notified when a workspace is closed (at least to my knowledge) so you can’t do any cleanup when that happens.

The alternative, then, is to do it when application exits. Now, if your cleanup includes deleting temporary files like it did in my case and it has to run on back end, you may be tempted to write a back end service to do this and call it from your front end (for example, in your front end application contribution’s onStop or onWillStop method or in an event listener that listens to onStateChanged event and calls the cleanup service when application state changes to closing_window as was suggested in Vince’s comment).
Don’t do this, it won’t work. No asynchronous call can be done from either of these places and your back end service simply won’t get called.

Instead, what you need to do is write a back end application contribution (so, a class that implements BackendApplicationContribution) and do your clean up in its onStop method.
Keep in mind that you still can’t have any asynchronous code there, everything has to be synchronous!
When the application is stopped, BackendApplication will gather all the backend application contributions in its onStop method and call each contribution’s onStop, if it’s implemented.

Obviously, “when the application is stopped” means different things depending on whether we’re talking about an electron app or a browser app. An electron app will get closed often, a browser app maybe not as often. If you’re writing a browser app, you may have to consider some other way of doing your cleanup. Then again, depending on your use case and implementation, this may be a non-issue for you.