Deleting uploaded files after session timeout

Hi, I’m working on a tool that should help users to convert certain scientific files. They can upload the files, which are then stored in a folder corresponding to their session ID. They can then choose the conversion they want, and get a download for the new files (which are in a subfolder of the session ID folder).

Now after the session has terminated, all files of that session are supposed to be deleted. I tried doing this via UI.getCurrent().addDetachListener(l->{FileUtils.deleteDirectory(sessionFolder);});
This doesn’t seem to do anything though, at least not in dev mode.
Am I doing something wrong? Is there a good practice way to deal with temporary session files?

You listen for the SessionDestroyedEvent

@Component
public class SessionExpiredListener implements ApplicationListener<SessionDestroyedEvent> {

    @Override
    public void onApplicationEvent(SessionDestroyedEvent event) {
       // delete the files
    }
}

UI detach is for things that are tied to the browser tab. There could be several UIs within same session. At the very latest the UI detach should happen when the session is destroyed. At least one browser tab must be open for them to be cleaned up earlier than that.

after what period of time, per default, can I expect this event to occur? In my dev environment it doesn’t seem to trigger

After the expiration of the HTTP Session.

Default is 30 minutes. You can test it by changing this value to something shorter

server.servlet.session.timeout

ah thanks! I expected it to be shorter! no wonder nothing kept happening