I want just delete the element in the array but not reformat the array to close the gap

delete array[index];

How costly is this assuming bytes32 as the arrays type? Will it call STORAGEKILL and free ether?

  • why don't you write some test code and find out? – Paul S Jul 12 '16 at 19:48
  • yeah will do that! – Roland Kofler Jul 12 '16 at 19:53


contract C {
    uint[] array;
    // omitted parameters
    function func() {
        delete array[1];

Leads to this snippet:

  tag 7         array[1]
  JUMPDEST          array[1]
  POP           delete array[1]
  PUSH 0            delete array[1]
  SWAP1             delete array[1]
  SSTORE            delete array[1]

Conclusion: taking Shayan's info "a 15,000 gas refund when a non-zero value is set to zero" I infer that the deletion without recompressing the array is a good way to save gas.

| improve this answer | |
  • So far I haven't found a good way to pre-estimate the gas costs to a good precision, the best way would be to run semi-dry runs, in the sense that to have public functions and send transactions to figure out how much gas they use. – Shayan Jul 13 '16 at 15:59
  • With browser-solidity you get instant feedback. The other leg is to know if you should leave gaps or what's the best pattern... – Roland Kofler Jul 13 '16 at 16:02
  • 1
    yeah but the feedback you get from browser-solidity is not quite right when you are using libraries or multi-contract smart contracts. it's alright for basic stuff – Shayan Jul 13 '16 at 16:56

20,000 gas when a value is set to non-zero from zero; 5,000 gas when writing to existing storage or setting a value to zero; and a 15,000 gas refund when a non-zero value is set to zero.

| improve this answer | |
  • I'm wondering if the solidity code above does exactly this? – Roland Kofler Jul 12 '16 at 15:50
  • source please ? – 0TTT0 Mar 17 '18 at 17:34

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.