4

Following answer and solidity_link says that:

Use delete on arrays to delete all its elements.

delete dataArray;

This is exactly the same as dataArray.length = 0


From this answer as I understand that negative gas costs will be 15000.

The only two OPCODEs with negative gas costs are STORAGEKILL(-15000) and GSUICIDEREFUND(-24000).

[Q] Overall, after deleting an array; how much gas refund will be made? Would it be only 15000 or 15.000 * 'length_of_the_array' ?

=> For example, I have an array with length of 100. When I delete it how much gas will I be refunded?

If only 15000; then should I traverse all the elements in an array and delete them rather than doing delete dataArray?

Something like:

for(uint i=0; i< dataArray.length; i++){
     delete dataArray[i]
}

=> For example, if I want to delete an mapping that maps a struct (uint[] that has 100 items), would it refund 15000 or the gas cost of the struct that is pointed (15000 * 100).

If only 15000; then should I traverse all the elements mapped by the element as well and delete them rather than doing delete mappingData?

mapping(address => uint[]) mappingData;  //'0xabcd' maps to length of the `uint[]` is 100
delete mappingData;

Something like:

for(uint i=0; i< mappingData[0xabcd].length; i++){
     delete mappingData[0xabcd][i];
}

Some observation: If I traverse an delete and delete items one by one, traversing will charge me additional gas cost, maybe equivalent to the refunded gas so where would be the motivation to use delete?

  • 1
    Perhaps what you're missing is that delete dataArray/dataArray.length = 0 will also walk through the array and delete each removed element. So there should be no difference between the two in terms of the total gas refund. – user19510 Apr 19 at 19:26
  • 1
    For a mapping you would have to delete individual values yourself, as there's no way to enumerate them automatically. – user19510 Apr 19 at 19:27
  • 1
    Also note that the gas refund can only offset up to half of the transaction cost, so it's certainly possible for deleting extra elements to have a net result of costing more. This is especially true if the only thing your transaction is doing is deleting storage. – user19510 Apr 19 at 19:29
  • As I understand, it is not recommended to do only delete operation in a transaction and its better to do delete operation where in a transaction which has a high gas usage. @smarx – alper Apr 19 at 21:09
  • Yes, you understood correctly. – user19510 Apr 19 at 21:20
2

Opcode STORAGEKILL refund only applies to a single slot.

Deleting an array in solidity is equivalent to deleting all slots used by the array. It should be 15000 * (1 + array_length * item_slots).

Here ìtem_slots is how many storage slots use each item in the array. For example far an uint256 array each item use one slot. An array of struct with two ùint256 will use two slots.

For more complex structs you will have to check the solidity documentation how structs are layout in storage to determine how many slots use each item.

For reference each storage slots uses 32 bytes.

You should be aware the refund is done at the end of the transaction, you have to have enough gas to process each slot upfront. Also your refund can be at most half of the gas used.

  • How is the case on mapping still does it delete all the slots? and how item_size is calculated and why it is divided by 32? @Ismael – alper Apr 19 at 21:00
  • As I understand, if a function does only delete dataArray it won't refund the gas used on the previously transactions that actually set the storage. It will refund the gas used on the transaction that only does delete dataArray, and I will charge me additional gas to delete the storage; so what would be the meaning of it ? @Ismael – alper Apr 19 at 21:06
  • 1
    @alper You cannot delete a whole mapping since keys are not stored and you cannot iterate over it. The motivations behind the current refunds gas policy is better as a separate question. – Ismael Apr 19 at 21:46
  • How about if I delete a struct? would it also iterate all variables under it and assign them zero? @Ismael – alper Apr 20 at 11:25
  • 1
    It should set to zero all members of the struct. – Ismael Apr 20 at 15:01

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.