1

This question already has an answer here:

Here's a contract:

pragma solidity ^0.4.19;

contract StoreAndFree {
    mapping(int=>int) myStore;
    function StoreAndFree() public {
        for (int i=1; i<100; i++) {
            myStore[i] = i;
        }
    }

    function kill() external {
        selfdestruct(msg.sender);
    }
}

When you kill it, what happens to the stored values? Are they still stored? Should you reset them to zero? Do you get gas back for resetting them to zero?

marked as duplicate by Ismael, Achala Dissanayake, Roman Frolov, eth Feb 5 '18 at 21:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

The opcode was officially renamed to SELFDESTRUCT in EIP-6. SELFDESTRUCT, however, wont give you the refund for the rest of the storage. Since storage is actually implemented as a hashmap, the EVM has no way of statically knowing, without being told, which storage slots belong to a contract. If you want the gas refund of resetting storage slots, you'll have to do it manually before calling SELFDESTRUCT.

Note: The max gas refund is half of what you spend, so zeroing a lot of storage slots is likely an altruistic behavior unless it's all happening in a very expensive transaction.

  • This conflicts with "the storage and code is removed from the state" at solidity.readthedocs.io/en/develop/… – William Entriken Feb 4 '18 at 21:06
  • It depends on how you look at it. The SELFDESTRUCT opcode, at the end of the day, doesn't refund you for the storage slots that will no longer be accessible. Nodes can choose to, and do, prune the storage that becomes inaccessible because of a SELFDESTRUCT, but it's not enforced. – flygoing Feb 5 '18 at 0:28

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