# How to transfer the whole balance of an account?

If I have an account with 5 ETH, can I send the full account balance of 5ETH to another account and have exactly 5ETH received on the second account ?

I understood that it will fail because I have no ether left to pay for the transaction fees.

If I want exactly 5 ETH to be received by the second account, I think I should have around 5,0002 ETH in my account to be able to transfer 5 ETH. Can you confirm?

• I edited your question to make both answer able to answer the same way as both were saying the same thing but one started by no and the other by yes 🤔 Jun 7, 2016 at 15:43

No.

You cannot send the entire balance because the fees are deducted from your account's balance, not from the value being sent. To sweep an account you need to send balance-gas*price ETH as the value, and also manually set the gas and gasPrice to ensure that you exactly zero your account.

E.g. If you have 5ETH and want to sweep it:

• A plain account-to-account transaction costs 21000 gas
• The current minimum gas price in the network is 20 GWei
• Set value to 5ETH - 21000*20GWei = 5ETH - 42K GWei = 4.999958ETH
• I always wondered why there wasn't a built-in function in the client to calculate that amount ;) Sure it's simple enough, but for beginners it's like difficult as it involves calculating things with new unknown quantities ! Jun 7, 2016 at 15:32
• @euri10 and with unit multiplier differences. I still can't figure how to calculate fee in ether from Wei gas price... I agree, should be done in the wallet. Jun 7, 2016 at 15:46
• 20 Gwei is not the minimum gas price. It's the default gas price. You can sent transactions with a gas price lower than 20 Gwei. Jun 7, 2016 at 21:18
• 21k times 20 isn't 42k, it's 420k. I think you've missed a zero in your answer, and the value needs to be 4.99958 instead of 4.999958. Doesn't it? Jul 23, 2016 at 4:09
• Sorry for crashing the party late, but IF the address you are sending to is a payable contract (not external account), gas should be set to 22000. Also, a brand new account (nonexistent acct, aka no history in trie) will have a cost of 25000. I would suggest editing response. Aug 15, 2019 at 1:01

Anytime you want to perform a transaction, you'll have to pay a fee for the computation involved in validating and mining your tx. If you do not have enough gas, you cannot send your tx to the network. In your example, send "all" and the receiver will get 5 minus fees.

My suggestion would be to automate it.

First, set the account and unlock it after launching geth attach:

var accountNum = 0
personal.unlockAccount(eth.accounts[accountNum]);

Now

var amount_to_send_eth = web3.fromWei(eth.getBalance(eth.accounts[accountNum]), "ether");
var amount_to_send_wei = amount_to_send_eth *1000000000000000000
var transactionFee = web3.eth.gasPrice * 21001;
var total_amount_to_send_wei = transactionFee + amount_to_send_wei

After that, a TXID is returned, that can be used to check if the transaction completed.

I'm a beginner, so hints how to imrpove it further are appreciated!

• Thnx, I help this Aug 30, 2019 at 23:03
var transferAll = function(from, to) {
var gas = '30000';
var gasPrice = eth.gasPrice;
var gasTotal = gasPrice.mul(gas);
var totalValue = eth.getBalance(from).sub(gasTotal)-1;
console.log("Transfer :"+totalValue+", from "+from+" to "+to);
personal.unlockAccount(from);
var result = eth.sendTransaction({from: from, to: to, value: totalValue});
console.log("Transaction hash:", result);
};

write a demo here

I test my script in kucoin community chain testnet. it's all pass. You can just replace the info of provider, it's also well done in ethereum network.

• Please share the code directly in the answer, not as a lnk. Apr 29, 2023 at 12:53
• While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Apr 30, 2023 at 15:04