Title says it all. I have a contract already deployed on the blockchain. Unfortunately I need to do some changes to it so will need to re-deploy it, and the previous one will become useless. I'd like to call selfdestruct() on it to save some gas costs on the re-deployment of the new one, but not sure how to do it. Should I code it in the migration file that I will use to deploy the new contract? It currently looks like this:

const MainContract = artifacts.require("MainContract");
const myLibrary = artifacts.require("myLibrary");

async function doDeploy(deployer) {
    await deployer.deploy(myLibrary);
    await deployer.link(myLibrary, MainContract);
    await deployer.deploy(MainContract);

module.exports = (deployer, network) => {
    deployer.then(async () => {
        await doDeploy(deployer);


  • I think it does not work like that. There is natively no way to deploy a new contract and call another function (directly) of a contract in the same transaction
    – Majd TL
    Jul 31, 2021 at 11:54
  • 1
    Your new deployed contract should call the selfdestruct function of the old contract in its constructor, but I don’t think that it has the permission to do that. And after the London upgrade u don’t get gas back (not 100% sure about it)
    – Majd TL
    Jul 31, 2021 at 12:31
  • Hey that's actually a great idea. Indeed the new address wouldn't have premission to call selfdestruct in the other contract (I'm using a modifier), but if I deploy the new contract with the same owner of the other one, and then call selfdestruct() with delegatecall in the constructor I think it should work, right? Thanks!
    – Hiperfly
    Jul 31, 2021 at 13:20
  • I’m 99% sure it does not work like that, I don’t remember exactly how delegate works :) anyway you need to try such a thing on a test net or locally first
    – Majd TL
    Jul 31, 2021 at 18:15


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.