Blog: facebook

Delete all members of a Facebook group and delete a facebook group

Facebook groups has been around for a long time. Facebook groups has been mostly used for selling things online and is a good alternative for sites like ebay and other selling/action sites basically because it is free and you mostly sell to your target market.

Some people like me admin/own some facebook groups and there are instances that we want to delete a group to create a new one or simply remove the group since it is not being used anymore. Unfortunately for facebook there is no automated way of doing it within their website.

To delete a facebook group, you have to first remove all the members of the group and then you will have to leave the group to complete remove that group. If you have like 50k+ members, removing them one by one is such a big task and will eat up a lot of your time.

Here is an automated way to remove the members of your group. You will need to either use google chrome or mozilla firefox and leave it open while the script is running.

  1. Open the facebook group and browse the members link
  2. Scroll to the bottom of the page and click on "see more"
  3. Now open up your browser the console for your browser. For google chrome the shortcut is "ctrl + shift + j" and for firefox it is "ctrl + shift + k"
  4. paste the code below in the console and let it run and do it's own thing
var deleteAllGroupMembers = (
    function () 
    {
        var deleteAllGroupMembers = {};
        // the facebook ids of the users that will not be removed.
        // IMPORTANT: add your own facebook id here so that the script will not remove yourself!
        var excludedFbIds = ['jessica.manoo.7']; // make sure each id is a string!
        var totalDeleted = 0;
        var usersToDeleteQueue = [];
        var scriptEnabled = false;
        var processing = false;
        deleteAllGroupMembers.start = 
        function() 
        {
            scriptEnabled = true;
            deleteAll();
        };
        deleteAllGroupMembers.stop = function() 
        {
            scriptEnabled = false;
        };
        function deleteAll() 
        {
            if (scriptEnabled) 
            {
                queueMembersToDelete();
                processQueue();
            }
        }
 
        function queueMembersToDelete() 
        {
            var adminActions = document.getElementsByClassName('adminActions');
            console.log(excludedFbIds);
            for(var i=0; i<adminActions.length; i++) 
            {
                var gearWheelIconDiv = adminActions[i];
                var hyperlinksInAdminDialog = gearWheelIconDiv.getElementsByTagName('button')[0];
                //var fbMemberId = gearWheelIconDiv.parentNode.parentNode.id.replace('member_','');
                //var fbMemberName = getTextFromElement(gearWheelIconDiv.parentNode.parentNode.getElementsByClassName('fcb')[0]);
                var fbMemberName = gearWheelIconDiv.parentNode.parentNode.parentNode.children[0].children[0].text;
                if (gearWheelIconDiv.parentNode.parentNode.parentNode.children[0].children[0].attributes[0] == null)
                    continue;
                var str = gearWheelIconDiv.parentNode.parentNode.parentNode.children[0].children[0].attributes[0].value;
 
                str = str.substring(str.indexOf('.com') + 5);
                fbMemberId = str.substring(0,str.indexOf('?'));
                if (excludedFbIds.indexOf(fbMemberId) != -1) 
                {
                    console.log("SKIPPING "+fbMemberName+' ('+fbMemberId+')');
                    continue;
                } 
                else 
                {
                    console.log('Queing: ' + fbMemberName+ ', (' + fbMemberId + ')');
                    usersToDeleteQueue.push({'memberId': fbMemberId, 'gearWheelIconDiv': gearWheelIconDiv});
                }
            }
        }
 
        function processQueue() 
        {
            if (!scriptEnabled) 
            {
                return;
            }
            if (usersToDeleteQueue.length > 0) 
            {
                removeNext();
                setTimeout(
                function()
                {
                    processQueue();
                },1000);
            } 
            else 
            {
                getMore();
            }
        }
        function removeNext() 
        {
            if (!scriptEnabled) 
            {
                return;
            }
            if (usersToDeleteQueue.length > 0) 
            {
                var nextElement = usersToDeleteQueue.pop();
                removeMember(nextElement.memberId, nextElement.gearWheelIconDiv);
            }
        }
        function removeMember(memberId, gearWheelIconDiv) {
        if (processing) 
        {
            return;
        }
        var gearWheelHref = gearWheelIconDiv.getElementsByTagName('button')[0];
        gearWheelHref.click();
        processing = true;
        setTimeout(
            function()
            {
                var popupRef = gearWheelHref.id;
                var popupDiv = getElementByAttribute('data-ownerid',popupRef);
                var popupLinks = popupDiv.getElementsByTagName('a');
                for(var j=0; j<popupLinks.length; j++) 
                {
                    if (popupLinks[j].getAttribute('href').indexOf('remove.php') !== -1) 
                    {
                        // this is the remove link
                        popupLinks[j].click();
                        setTimeout(function(){
                        var confirmButton = document.getElementsByClassName('layerConfirm uiOverlayButton selected')[0];
                        var errorDialog = getElementByAttribute('data-reactid','.4.0');
                        if (confirmButton != null) {
                        if (canClick(confirmButton)) {
                        confirmButton.click();
                        } else {
                        console.log('This should not happen memberid: '+memberId);
                        5/0;
                        console.log(gearWheelIconDiv);
                        }
                        }
                        if (errorDialog != null) {
                        console.log("Error while removing member "+memberId);
                        errorDialog.getElementsByClassName('selected layerCancel autofocus')[0].click();
                        }
                        else
                        {
                            console.log("Now Deleting "+ memberId + ', (' + totalDeleted + ')');
                            totalDeleted = totalDeleted + 1;
                        }
                        processing = false;
                        },1500);
                        continue;
                    }
                }
            },1000);
        }
        function canClick(el) 
        {
            return (typeof el != 'undefined') && (typeof el.click != 'undefined');
        }
        function getMore() 
        {
            processing = true;
            more = document.getElementsByClassName("pam uiBoxLightblue uiMorePagerPrimary");
            if (typeof more != 'undefined' && canClick(more[0])) 
            {
                more[0].click();
                setTimeout(
                    function()
                    {
                        deleteAll();
                        processing = false;
                    }, 4000);
            } 
            else 
            {
                deleteAllGroupMembers.stop();
            }
        }
        function getTextFromElement(element) 
        {
            var text = element.textContent;
            return text;
        }
        function getElementByAttribute(attr, value, root) 
        {
            root = root || document.body;
            if(root.hasAttribute(attr) && root.getAttribute(attr) == value) 
            {
                return root;
            }
            var children = root.children,
            element;
            for(var i = children.length; i--; ) 
            {
                element = getElementByAttribute(attr, value, children[i]);
                if(element) 
                {
                    return element;
                }
            }
            return null;
        }
 
        return deleteAllGroupMembers;
    })();
deleteAllGroupMembers.start();

Let me know in the comments section if this works out for you. I've used it just today and it seems to be working fine. Please take note that facebook updates their website from time to time and change the code, so the code above might stop working after sometime.