Understanding and Using the Delete Keyword in Solidity

solidity

The blockchain is an immutable, append-only transaction set.
I have the following questions regarding delete keyword –

Then what is the purpose of deletekeyword in solidity?

Is it only for variables stored in memory?

Is it possible to delete variables stored in storage and stack?

What are all the possible cases in which delete keyword should be used and where it should not be?

Best Answer

delete is actually not really deletion at all - it's only assigning a default value back to variables. It does not make difference whether the variable is in memory or in storage.

For example, issuing a delete to a uint variable simply sets the variable's value to 0.

delete will also give a bit of your used gas back - this is to encourage deleting variables after their data is no longer needed so the size of the blockchain would stay a bit smaller. Note that you can never regain more gas than what your current transaction is using.

You can read more about delete here: https://solidity.readthedocs.io/en/v0.4.24/types.html#delete

Related Topic