'How to logout from metamask account in reactjs using Ethereum

I am unable to disconnect from Metamask account. I am attaching the code snippet.

export const logoutUser = () => {
    if (window.ethereum && window.ethereum.isMetaMask) {
        window.ethereum.on('accountsChanged', function (accounts) {
            return () => window.ethereum.removeListener('accountsChanged', accounts);
        });    
    }
}


Solution 1:[1]

As of now, you cannot log out of Metamask by web3 or Ethereum api. You have to manually disconnect Metamask.

accountChanged listener will just detect if Metamask connects to a different account.

Solution 2:[2]

As of now we can't disconnect from Metamask from our app, but what developers do is to remove/add the information of that Metamask account in a variable inside that app by pressing the disconnect/connect button.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jasper
Solution 2 BaqirNekfar