I did some poking around and didn't see an answer... and I figure someone knows this off the top of their head.

Question: what is the gas cost associated with setting and deleting a storage variable in the same transaction? Since at the end of the tx, there is no new state, I vaguely recall reading once that the gas costs are different. Can anyone confirm this or point me at the documentation for this?

If there's no special treatment, I'd expect this to cost 20k (set) + 5k (delete) - 15k (refund) = 10k per variable, which is a lot...

Thank you!

Edit: to clarify, my hope is to have a mapping which I can use for essentially "flash storage" during a single tx. To my knowledge there is no memory equivalent of mappings, as they only exist in storage, which is why I don't think there's an alternative which uses only local variables.

  • Sounds like you don't need a state variable here. Commented Mar 18, 2020 at 6:56
  • 1
    Also, refunds can reduce the total gas used by max 50%. So 20 + 5 - 15 would cost max(25/2, 10) = 12.5 gas
    – Newti
    Commented Mar 18, 2020 at 18:18

2 Answers 2


I don't think there is any special treatment. The EVM is "stupid" in that sense that it only executes an instruction at a time and count how much gas was used (or refunded). So it just executes the first instruction, counts how much gas is spend, starts the next instruction and so on...

I really don't remember the exact gas costs but at least your idea is correct. The idea of delete refunds is not to make free transactions but to give a small incentive for developers to keep deleting data. And 10k is a lot less than 20k.


Instead of answering your question directly, I will take a different approach, because it sounds to me that you've started a work which involves using a storage variable, and by the time you finished, it turned out that this storage variable wasn't needed to begin with.

If the storage variable attains its original value at the end of the transaction, then you may as well copy this value into a local variable at the beginning of the function, and then use that local variable instead of the storage variable throughout the rest of the function.

This method is desirable not only when your storage variable's current value is 0, but also for any other case, since storage variable operations are significantly more expensive than local variable operations.

Your Answer

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

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