I have done some looking around and I cannot seem to find a current solution to my issue.

I would like to call an apex method to remove records when a user leaves a visualforce page.

I've gone down the path of an action function, and am currently in the middle of JS Remoting. I can get the javascript method to successfully call my controller method when I call it directly from my command button, but I cannot get it to run on the unload event, even when I can confirm that I entered that method via the console logs.

Page Script:


    $(window).unload(function () {
         console.log('I ran'); 

    function removeUserFromPublicGroup() {
        console.log('In method');
            function(result, event) {
                if(event.status) {
                else {


Controller method

public static void removeUserFromPublicGroup()
    system.debug('Remove Server Method called');

I tried the beforeunload event and in various ways and I cannot register that that event has even been fired via console messages or debugger statements. And where unload does run and I am in the removeUserFromPublicGroup method, the controller method never runs as confirmed by my debug logs from the Developer console.

I have referenced this stackexchange similar question however it does not seem to work for me at all. As in, I don't get a console statement or hit a debugger.

I have also referenced this page for events to use when leaving the page.

Just to reiterate calling it from a command button DOES work and WILL execute my controller method via JS Remoting. But I need to run the same method when a user leaves the page. Which is NOT working even though the remove method was entered.

<apex:commandButton styleClass="slds-button slds-button_neutral" id="cancelButton" value="Cancel" onclick="removeUserFromPublicGroup();  cancel('{!holder.Id}'); return false;"/>

1 Answer 1


Visualforce.remoting.Manager.invokeAction is almost certainly asynchronous in nature. I'm assuming by the time the unload function is completed the browser is about ready to kill everything on the page, including any outstanding async operations. So it never runs as the page is gone before it even gets a chance. See also - Doing an ajax call on window.unload

There is probably a more accurate way to describe that in terms of JavaScript event loops, the execution stack, and queued requests.

You will either need to switch to a synchronous operation or an entirely different mechanism to achieve what you want.

I'd probably go with the latter if you can. It's always going to be hit and miss trying to perform any action as the user transitions away from a page. A drop in internet connectivity, power loss, or any number of other scenarios could prevent your last second operation from occurring.

Instead, you could have a scheduled batch job find the records that removeUserFromPublicGroup() operates on. It could then do the required work regardless of the state of the users browser. If you are concerned about records being removed for active users, have something on the page that keeps updating a "last modified" field on the sObjects in question. The batch job could then leave the records for a period of time.

  • 1
    That's what I was afraid of. I wasn't sure if since the ajax call was invoked that it would run after the page was unloaded, which clearly it doesn't based on the behavior. This is an edge case scenario and I think the batch job to do any 'leftover' tasks is going to be what I have to do. Thank you
    – JRiffe
    Commented Nov 8, 2018 at 21:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .