🔮
Ethereum
  • General
    • What is Ethereum
      • Ethereum & Bitcoin General Comparison
      • Singleton State
      • The Ethereum Virtual Machine (EVM)
      • Opcodes (operation codes) EVM
      • Ethereum Client
      • Forks
      • Gas (wei)
        • EIP-1559
        • Table Conversion (wei)
      • Proof of Stake (PoS)
      • Proof of Authority (PoA)
      • The Beacon Chain
      • Networks
        • Ethereum mainnet
        • Goerli
      • Account-based model & UTXO-based model
      • Externally Owned Account (EOA)
      • Node Clients
        • Geth
        • Nethermind
      • Contract Account
      • Smart Contract Upgradeability
      • Ultrasound Money
      • Merkle Trees
        • Patricia Merkle Tree
      • Tries
        • State Trie
        • Storage Trie
        • Transactions Trie
        • Receipts Trie
      • Transactions
        • Ethereum Transaction Architecture
      • World State
        • Chain of States
        • Chain of Blocks
        • Stack of Transactions / Mempool
      • Contract Creation
      • Message Call Transaction
      • P2P Network
      • Web3.js
      • Ether.js
        • Smart Contract Interaction Example
      • Web3.js vs Ether.js
      • Node Providers
      • ENS (Ethereum Name Service)
      • Web3 dapp
      • Escrow
      • Multi-signature
      • ERC-20 tokens
        • Send ERC20s to Contracts
      • NFTs
        • ERC-721 and ERC-1155
      • Solidity
        • State Variables
        • Data Location
        • Numbers
        • Modifiers
        • View & Pure Modifiers
        • Data Types
          • Modifiers
          • Modifiers (Functions)
          • Address & Address Payable
        • Hardhat
        • Payable Functions
        • Receive Function
        • Fallback Function
        • Global Variables
        • Self Destruct
        • Create2 Function
        • Revert function
        • Require function
        • Assert Function
        • Calldata
        • Interface
        • Mapping
        • Array
        • Struct
        • Inheritance
          • Virtual & Overwrite
          • Multiple inheritance
          • Hierarchical Inheritance
        • Events
          • Indexed (keyword)
          • LOG0 - LOG4
        • Multi-signature Example
        • Smart Contracts
          • Context
      • Application Binary Interface (ABI )
  • Extras
    • Terminology
      • Bytecode
      • Keccak-256
      • Turing complete
Powered by GitBook
On this page
  1. General
  2. What is Ethereum
  3. Solidity

Self Destruct

PreviousGlobal VariablesNextCreate2 Function

Last updated 2 years ago

In Solidity, the selfdestruct function is used to destroy a contract and send its remaining ether balance to a designated address.

The syntax for selfdestruct is as follows:

scssCopy codeselfdestruct(address payable recipient);

The recipient parameter is the address where the remaining ether balance of the contract will be sent after the contract is destroyed. The recipient parameter must be a payable address, meaning it has a payable modifier and can receive ether.

It is important to note that once a contract is destroyed using selfdestruct, it cannot be recovered and any remaining data on the contract is permanently lost. Therefore, selfdestruct should only be used when it is necessary and appropriate.

When a contract is self-destructed using the selfdestruct function, the code of the contract is removed from the blockchain, but the state variables and the balance of the contract are not deleted. Instead, the remaining ether in the contract is sent to the designated recipient address specified in the argument of the selfdestruct function. The state variables of the contract will still be present on the blockchain, but they will no longer be accessible through the destroyed contract's address.

Instead of self-destructing the contract, you could disable its functionality by setting state variables so that nobody can call the function or send ether in the future. This can be done by adding a bool variable that is set to false when the contract is no longer needed. Then, in each function, you can add a require statement that checks if this variable is true. If it's false, the function will revert the transaction and prevent any further actions on the contract. This approach is safer because it doesn't delete the contract and all of its state and history from the blockchain, but rather simply makes it non-functional.

Even if a contract is removed by selfdestruct, it is still part of the history of the blockchain and probably retained by most Ethereum nodes. So using selfdestruct is not the same as deleting data from a hard disk.

Introduction to Smart Contracts — Solidity 0.8.19 documentation
Logo