Selfdestruct: A cleanup feature considered for Shanghai
The latest Ethereum Core Devs [meeting](https://www.youtube.com/watch?v=ZZx7d14vE10) took place on 10th November, Thursday. The meeting set a stage to discuss Shanghai EIPs, particularly proposals to include EIP-1153, 2537 and 2294 among others. Apart from these there were other important EIPs that were discussed with regards to [Shanghai](https://etherworld.co/2022/10/25/shandong-pre-shanghai-testnet/) that included a conversation around Selfdestruct function. But before finalizing any decision on whether to include or exclude EIP-4758, Andrew Ashkhmin from Eth Cat herders asked Ethereum Foundation to put a blogpost on Selfdestruct to indicate that the community is serious about considering the position of the EIP-4758. Such that any [DApp](https://etherworld.co/2018/02/05/what-are-dapps-and-which-are-the-best-ones-to-use-in-2018/) developer who rely on selfdestruct functionality should come up with their use cases. Only then can the developers can think about the mitigations.
Selfdestruct is basically a term used to free up space on blockchain by clearing all smart contracts. Selfdestruct is by far the only way to remove the contract from Ethereum. But recently, Developer MariusVanDerWijden [proposed](https://twitter.com/vdWijden/status/1574314890923212802) to add [EIP-4758](https://eips.ethereum.org/EIPS/eip-4758) to Shanghai that suggests to deactivate selfdestruct. He raised an objection by saying that the EIP-4758 changes the semantics of the SELFDESTRUCT opcode and asked to turn it into a SENDALL which sends the balance of the contract to a receiver. Basically the argument is that selfdestruct require to bring changes to code and removing storage. Going ahead, the future of Verkle trees make selfdestruct function incompatible. EIP-4758 brings that change. The proposal advises disabling the function as a requirement for statelessness.
Before moving forward and locking a decision on the position of selfdestruct functionality it is important to understand about selfdestruct first.
## What is Selfdestruct and why we need it?
Selfdestruct is a term provided by [Ethereum](https://etherworld.co/2017/08/13/a-complete-guide-to-understand-ethereum-for-beginners/) to describe termination of a contract. It requires making changes to the state of an account, particularly by removing all code and storage. This function is used whenever an emergency occurs like in case of an attack. Selfdestruct function helps developers to remove [smart contracts](https://etherworld.co/2017/09/18/smart-contract-an-introduction/) from the [blockchain](https://etherworld.co/2017/02/08/blockchain-beginners-guide-part-1/) and transfer Ether balance to whatever address they specify.
The Ethereum blockchain technology provides an immutability feature for smart contracts. This means that all of the smart contracts are self-executed and cannot be changed after it is deployed on the blockchain. However this makes the blockchain prone to attacks as well because a [smart contract](https://etherworld.co/2017/06/05/minereum-the-first-self-mining-smart-contract/) can be executed by anyone on the permission-less blockchain. Secondly, all the data stored on the blockchain, transactions, and bytecode of [smart contracts](https://etherworld.co/2017/09/18/smart-contract-an-introduction/) are visible to the public, which makes smart contracts become attractive targets for attackers.
One such case happened in the year 2016 after which the community realised to built a feature like selfdestruct. It was the time when [decentralised organisations (DAOs)](https://etherworld.co/2018/02/05/what-are-dapps-and-which-are-the-best-ones-to-use-in-2018/) were in their growing stage. DAO refers to Decentralized Autonomous Organization that operates through smart contracts. In the year 2016 an unknown attacker extracted 3.6 million Ether from a decentralized fund called TheDAO which was built by a few members of the Ethereum community. It pointed out the flaw in its code that allowed attacker to drain funds from the main platform. A loophole in the coding had led the attacker to ‘ask’ the smart contract [(DAO)]() to return Ether multiple times before the smart contract could update its balance.
The attack continued for days due to the immutability feature of smart contracts. There was no way to destroy a contract at that time. Therefore the DAO developers thought it was right to fork the entire blockchain at that time to further prevent such attack and exploitation. To come up with a solution, selfdestruct was designed to created a protective shield from security threats. It was first called as ‘suicide function’ but later renamed as selfdestrcut.
It is important to note that selfdestructing the smart contract is not the same as deleting data from your hard disk. Imagine you have created a smart contract. It contains a few Ethers but now you do not want them or maybe there is a bug in it. In this case, one can selfdestrcut the smart contract by self destructing the address is more convenient as it cause far less gas fees. Keep in mind that even if a smart contract is removed by selfdestruct it still remain a part of blockchain’s history. Also, if someone sends Ether to the removed contracts, the ETH is lost.
##How selfdestruct is used by the developers?
Selfdestruct function is used by developers for a range of purposes. Developers mainly use selfdestruct function to make smart contract code more secure by upgrading the smart contracts. It is useful to clean up unused contracts and transfer Ethereum balance. Let us understand by an example. The ERC-20 framework is the implementation of standard API for fungible tokens on Ethereum within smart contracts. It is a standard interface for tokens. Any token that does not interface with the [ERC-20](https://etherworld.co/2018/03/28/coinbase-announces-support-to-erc-20/) standard will have difficulty interacting with other contracts. In such a case, developers will not make changes in the current contract or upgrade it. Instead, they will build a new contract with new required functionalities.
Further, according to data collected by 2021 [selfdestrcut smart contract report](https://arxiv.org/pdf/2005.07908.pdf#:~:text=On%20the%20one%20hand%2C%20using,an%20attack%20vector%20for%20attackers), about 55% respondents use selfdestruct to stop the contracts when security vulnerabilities are detected in their contracts. Around 33.33% use it to clean up the blockchain. 27.27% use it for transferring Ethers. On the other hand 12%, 6% and 3% of the respondents use selfdestrct feature for upgrading contract, business requirement and gas refund respectively.
CONCLUSION