In this example contract: https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/token/ERC721/ERC721Token.sol

The removeToken method is supposed to delete a token; the token _tokenId no longer exists. So why does it do tokenOwner[_tokenId] = 0; instead of delete tokenOwner[_tokenId];?

Surely setting the value to 0 does not actually delete it? Wouldn't that mean that the contract will always take more storage space, never going down, even when the tokens are burnt?


As there is no null in Solidity all variables are considered to be empty if their value is 0 or similar (I guess "" for strings). So setting an integer to 0 basically deletes it.

delete also doesn't do anything else than set the value to 0 for integers. So doing tokenOwner[_tokenId] = 0; is equal to delete tokenOwner[_tokenId];

  • Is it the same in terms of gas? Is setting a variable to 0 costs negative gas (returns gas to sender)?
    – takeshi
    Jun 22 '18 at 10:12
  • 1
    You never get gas actually returned to you but yes, setting variable to its default value reduces gas usage of the transaction Jun 22 '18 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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