I'm reading bits and pieces that selfdestructs within contracts are a good thing. Can someone explain what the benefits are of doing a contract selfdestruct when it comes to Ethereum programming? Is there a difference if you're using different clients or implementations, or languages?
Selfdestructs in Ethereum are an operation (an OPCODE actually) at the EVM level, independent of what language or client you are using.
For example, calling
selfdestruct(address) sends all of the contract's current balance to
This is useful when you are finished with a contract, because it costs far less gas than just sending the balance with
In fact, the SELFDESTRUCT opcode uses negative gas because the operation frees up space on the blockchain by clearing all of the contract's data.
This negative gas deducts from the total gas cost of the transaction, so if you're doing some clean-up operations first, SELFDESTRUCT can reduce your gas costs.
Note: this question originally asked about the
suicide opcode, which has become
selfdestruct in the meantime as per EIP6.
selfdestruct is the encouraged term and may be found in newer Solidity and Serpent code. It is the same API and behavior, as described in the other answers, and is an alias for
EIP6 describes the motivation:
The primary reason for us to change the term suicide is to show that people matter more than code and Ethereum is a mature enough of a project to recognize the need for a change. Suicide is a heavy subject and we should make every effort possible to not affect those in our development community who suffer from depression or who have recently lost someone to suicide.
selfdestruct() function and the relative OPCODE are used when you have a contract that has a bug or an unwanted behaviour and you want to get rid of it.
selfdestruct(address) is a better version as @tjaden-hess pointed out, when you have a payable contracts (contracts that receive ethers) the ethers will be redirected to
address which is a big win.
When you interact with a selfdestructed contract NOTE this: If you send a transaction and/or funds to it then your funds are LOST. Be sure to not send funds or other transactions to it after suiciding a contract.
That's an issue you always have to keep in mind when developing apps that use contracts with a selfdestruct function, be sure the address of the contract is removed from your app after
selfdestruct() has been called on the contract, maybe you can always call a getter method to see if the contract responds with a valid value before sending a transaction and/or transferring ethers to it.
The name of the method is now
We have some description of it in the new solidity docs: http://solidity.readthedocs.io/en/v0.4.21/units-and-global-variables.html?highlight=selfdestruct#contract-related
Here's a post from r/ethereum about suicide with more infos and a small discussion about it.