Disconnect a web3 wallet [Solved]


I’m looking for the correct function call to disconnect my dapp from a web3 session, specifically when using walletconnect but also with a metamask connection would be handy. I’ve read all the docs, and also scoured the internet (trying various combinations of killSession(), disconnect() etc. What I am looking to implement is the ability for my users to explicitly disconnect the wallet connection (much as uniswap does with it’s ‘disconnect’ option.

Any advice gratefully received.


1 Like

All credit to today’s hero @Erno on discord, but for anyone else stuck here is the answer:

What you can do is access the WalletConnectProvider (once a connection has been made), via Moralis.Web3.activeWeb3Provider.provider. I think you can find the methods you need on that object. For instance, you could use .deactivate(). (make sure that Moralis.Web3.activeWeb3Provider.provider is defined, as this is only set after enabling web3 / authenticating)

It will return a walletconnect web3 provider instance, documented in https://docs.walletconnect.org/quick-start/dapps/web3-provider


I couldn’t get the above approach to work, but it set me on the right path for sure! I have found the below code to be effective (obviously not called one after the other. . . :)):

window.web3 = await new window.Moralis.Web3.enable({ provider: "walletconnect" });

await window.web3.eth.currentProvider.disconnect();

@keyoke Amazing work! You can further look into Moralis documentation for Events : https://docs.moralis.io/moralis-server/web3/web3-1#events

onDisconnect Event will help you watch disconnect your wallet

const disconnect = Moralis.Web3.onDisconnect(function(accounts) {

Any questions and queries feel free to ask

Thanks @Moralis_DAO, I really appreciate the reply. I’d read that doc in detail (love all the docs, great job team moralis), and that’s a wrapper for watching an event, not calling the disconnect method itself. I need something to call disconnect, not watch for the event. I think what you are proposing is a different thing to my requirement.

Apologies for not understanding your requirement.

Yes, you can use web3.eth.currentProvider.disconnect() to disconnect from wallet and onDisconnect to add listener to the disconnect.


Exciting! Looking through the Moralis js code and I found the function ‘cleanup’. The below works very nicely to close a web3 connection:

await window.Moralis.Web3.cleanup();

1 Like