You may or may not be used to a garbage collectors in your previous programming language. There is no such thing in Solidity and even if there was a similar concept, you would still be better off managing state data yourself. Only you as a programmer can know exactly which data will not be used in the future anymore.

You may be wondering 'Why should I care?'. Let us take a look at the reasons for releasing unused data.

1. Gas costs

You can receive a gas refund for certain storage-clearing operations, but the amount depends on the slot’s original and current values. Since the London upgrade, EIP-3529 sets the refund for an eligible nonzero-to-zero storage change to 4,800 gas and caps total transaction refunds at 20% of gas used. The old 15,000-gas refund and “75% back” calculation no longer apply. Refunds are accounted for after execution; they do not provide extra gas while a function runs.

2. Network bloating

If the gas costs did not convince you, I hear you. It seems like a design flaw in Ethereum that you can just store data and leave it there forever without ever paying for it again. But that does not mean should store everything forever. Unused values add to the live state that full nodes maintain. Clearing a value does not erase its historical appearance in the blockchain. What a waste of resources!

In fact, new rental models have been discussed, even sophisticated methods including revival methods. That 1–2-year prediction belonged to the original 2019 discussion; it is not a current roadmap forecast, but if your contract should be as future-proof as possible, you better keep your storage usage low!

How to free up unused data

Now to the practical part. Freeing up data is really easy and there is even a special keyword delete in Solidity to help you.

Deleting simple types

For simple types like integers, it does not really matter if your write delete myInteger or myInteger = 0. It will have the same effect. Depending on the context, you may choose one way over another. Generally, if it is about some calculations = 0 is easier to read and if it is only about freeing up storage delete will be easier to read.

Deleting arrays

For clearing an entire array, delete resets its contents, but the work can grow with the array size. Large storage arrays may need bounded cleanup across several transactions. It will automatically create an array of length 0 for dynamic arrays or set each item of the array to 0 for static arrays. If you want to delete just a single item like delete myArray[index], you should be aware that this will create a gap in the array. To avoid the gap and if you do not care about the order of the items, what you can do instead is myArray[index] = myArray[myArray.length - 1]; followed by myArray.pop(). Pop removes the last element and also implicitly calls delete on the removed element.

Deleting structs

Instead of having to write myStructInstance = MyStruct(0,0,address(0)), you can just write delete myStructInstance. It will automatically clear out each entry of your struct with one exception:

Deleting mappings

If you have a mapping in a struct or in your state in general, Solidity cannot delete it, because it does not know the keys for the mapping. Since the keys are arbitrary and not stored along, deleting each mapping entry requires knowing its key. A value can then be deleted by delete myMapping[myKey].