Design Pattern Solidity: Free up unused storage

Why you should clean up after yourself

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

The obvious is answer is that you can receive gas refunds for releasing unused storage. In the yellow paper on page 25 'Appendix G. Fee Schedule', you can read the gas costs for each instruction. As you might know, SSTORE will generally create the most costs in your contracts with a significant cost of 20,000 gas per instruction. On the contrary, if you look at R_sclear:

Refund given when the storage value is set to zero from non-zero.

15,000 gas refund means you can actually get 75% of your storing costs back! That is a large amount, do not forget about this. And the solution is simple, just set a value back to 0 once you are sure it will not be used anymore.

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. Storing useless data only forces every Ethereum node to store your data for all eternity. What a waste of resources!

In fact, new rental models have been discussed, even sophisticated methods including revival methods. I do not see those coming any time soon (within the next 1-2 years), 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

But for deleting arrays, you always want to use delete. 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 = new 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, the only way to delete structs is to know the key for each stored value. A value can then be deleted by delete myMapping[myKey].


Markus Waas

Solidity Developer

More great blog posts from Markus Waas

  • How to use ChatGPT with Solidity

    Using ​the Solidity Scholar and other GPT tips

    Welcome your new best friend as a Solidity developer: ChatGPT. If you're a developer and not using ChatGPT yet, then what the hell are you doing? Let's explore some unique ways this can help you as a Solidity developer. You can just chat directly with ChatGPT or you can use custom GPTs. I have...

  • How to ​integrate Uniswap 4 and create custom hooks

    Let's dive into Uniswap v4's new features and integration

    Uniswap v4 adds several key updates to improve gas efficiency, customizability, and functionality. So let's not lose time and dive into it! By now you've probably heard of Uniswap and so-called AMMs (automated market makers). But if you're not familiar with Uniswap yet, it's a fully decentralized...

  • How to integrate Wormhole in your smart contracts

    Entering a New Era of Blockchain Interoperability

    Wormhole began as a token bridge between Ethereum and Solana and has since expanded into a decentralized interoperability protocol for multiple blockchain ecosystems. Wormhole now supports many chains like Ethereum, Cosmos, Polkadot, Injective and many more. It makes cross-chain communication...

  • Solidity Deep Dive: New Opcode 'Prevrandao'

    All you need to know about the latest opcode addition

    Let’s back up for a second and figure out what has changed since ‘ The Merge ’. The upgrade finally brought a new consensus mechanism to Ethereum. Instead of the old Proof of Work, blocks are now produced via Proof of Stake . Proof of Work finds consensus via block hashes and a process called...

  • How Ethereum scales with Arbitrum Nitro and how to use it

    A blockchain on a blockchain deep dive

    Have you heard of Arbitrum Nitro? The new WAVM enables Plasma but for smart contracts in a super efficient way! It enables having a side chain with guarantees of the Ethereum mainnet chain. Arbitrum has already been one of the most successful Layer 2s so far, and the new Nitro is a major upgrade...

  • The Ultimate Merkle Tree Guide in Solidity

    Everything you need to know about Merkle trees and their future

    Most of you probably have heard of Merkle trees by now. They are used everywhere in the world of blockchain. But are you really sure exactly How they work? What the best ways to use them are? What the future holds for Merkle trees? This is not a Merkle tree. What are Merkle Trees? Ralph Merkle...

  • The New Decentralized The Graph Network

    What are the new features and how to use it

    Quite some time has passed since my last post about The Graph. If you don't know what it is and why it's useful, go and read the post. It's still relevant and explains in detail why it's needed and how to use it with the centralized hosted service. But the tl;dr is: Events on a blockchain are a...

  • zkSync Guide - The future of Ethereum scaling

    How the zero-knowledge tech works and how to use it

    Have you heard of zkSync and its new zkEVM? The new zkSync EVM enables Zero-knowledge proofs for any smart contract executions. What does that mean? Well read on later. But what it enables is having a side chain with similar (not not exact) guarantees of the Ethereum mainnet chain. How cool is...

© 2024 Solidity Dev Studio. All rights reserved.

This website is powered by Scrivito, the next generation React CMS.