You can use Javascript in the onbeforeunload event and then execute an AJAX request with the asynchronous disabled, so it "blocks" the browser before the user leaves
You can use jQuery to call async AJAX, for example:
window.onbeforeunload = function(){ $.ajax({ url:"your_delete_script.php", async:false }); }
or you can create an image on the fly that acts like a beacon to execute a script.
window.onbeforeunload = function(){ var img = new Image(); img.src = "script.php"; }
just make sure your script receives ignore_user_abort in this part, so it will be executed independently if the user closed the browser or didn’t execute
source share