1

If I delete the whole array (static/dynamic), then all elements of an array are set to default value. So, it means that if an array has a million elements, then I have to pay gas for setting up of million elements to zero, and likely exceed the limit of Ethereum.

However, delete in blockchain is different than delete in a memory of a machine. Nothing can be deleted from blockchain ever, and thus the only reason why delete operation may exist is just "to mark some field to be invalidated" for the purpose of a smart contract code. There is no other reason. In the case of value types (int, float, etc), it does not matter whether we just invalidate by zeroing the storage variable or adjusting its property. However, The problem comes with arrays - there is no reason to zero all the fields of an array just due to their invalidation. Instead, just the length attribute of an array should be set to zero, and index check for accessing an array added.

Am I wrong?

1 Answer 1

2

For storage arrays, there is some use in deleting elements. Every storage address that is set to zero from a nonzero value produces a gas refund of 15000 gas, which can offset the costs of other operations, such as storing a new value.

For specifics see What are the limits to gas refunds?.

In many cases, however, you are correct that deleting elements is not cost-effective and so in practice most of the time data in smart contracts is not deleted.

4
  • Also for security reasons, if you delete something an array you do not want for the content to be accesible in another way later.
    – Ismael
    Nov 1, 2018 at 18:45
  • @Tjaden: I see that gas refund is very poorly incentivized, and moreover it is applicable only for some use cases. I consider an array of let say 1000 items and want to just "delete the array". But, instead of paying just for adjusting the length of the array (one storage variable), I have to pay 1000x for SSTORE, and the refund is really ridiculous to that cost. So, instead of using native arrays, I am more incentivized to create a wrapper of efficient array built over native arrays. This is very unhappy to me. Nov 2, 2018 at 11:58
  • I agree that it would make sense to have separate deletion/length reduction semantics. Believe me, this is far from the only place where solidity could be better designed. Nov 2, 2018 at 14:26
  • BTW, you can manually set the length to 0 using 1 line of assembly, which might be the best option here Nov 2, 2018 at 14:27

Your Answer

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

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