Note that on desktop, you're not guaranteed against crashes (app crashes, system crashes, battery out of power, etc.) and so it is customary for desktop application to have a periodic save of the application state, or some other recovery policy.
Also on web, IIRC you can always open a background job (with workers) that is not tied to the browser window to do cleanups. (To be verified)