Hi @rkurniadi
I wrote this small contract for you, I am going to elaborate above and you can also try it while reading my explanation.
pragma solidity 0.8.0;
contract TestSD {
uint public number = 10;
function setNumber (uint _n) public {
number = _n;
}
function kill () public {
selfdestruct(payable(msg.sender));
}
}
Consider the contract above.
It has a variable called number
which is set to 10 by default.
It has a function setNumber()
that just sets number
to whatever uint you will pass to it.
Notice the function kill()
, by calling it you also trigger selfdestruct()
.
What selfdestruct()
does is explained in the Solidity documentation (source)
The only way to remove code from the blockchain is when a contract at that address performs the selfdestruct
operation. The remaining Ether stored at that address is sent to a designated target and then the storage and code is removed from the state. Removing the contract in theory sounds like a good idea, but it is potentially dangerous, as if someone sends Ether to removed contracts, the Ether is forever lost.
By calling selfdestruct()
you are not deleting a contract from the blockchain, you are basically set it back to default values which mean that all the data saved are lost.
If you try to call the function kill()
in my contract above, you will see that number
will be set to 0.
You will also not be able to call any function in that contract anymore.
Cheers,
Dani