I'd like to implement a self-destruct method for a smart contract that pools money from people as a last resort.

Before calling self-destruct, I'd like to refund all the money to the users and send whatever is remaining to the owner address.

I'm thinking of something along the lines of

uint256 usersLength = users.length;
for (var i = 0; i < usersLength; i++ ) {
    address user = users[i];
    uint256 refundAmount = pooledMoney[user];

I'm a little worried what would happen if the size of the users array is very large, will the method run out of gas within the loop?

If so, what are alternative solutions for implementing something like this?

1 Answer 1


Yes, the transaction will run out of gas if the array of users is too big.

The alternative is for each user to retrieve its part of the pool.

function withdraw() public {
    uint refundAmount = pooledMoney[msg.sender];
    if (refundAmount > 0) {
        pooledMoney[msg.sender] = 0;
        emit Refunded(msg.sender, refundAmount);

After all users have withdraw their ether then the owner can destruct the contract.

  • Thanks for answering! Was wondering if their was an efficient way to do this on the users' behalf, but I guess there isn't... Commented May 10, 2018 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.