remove_code

Call v334 → current #4

Removes previously uploaded contract code if no instances exist.

View calls on chain

Click items to navigate. Pan and zoom to explore.

Used by: developers

The Big Picture

Uploaded code consumes storage and locks deposits. If no contracts use the code anymore, the uploader can remove it to reclaim their deposit. This is housekeeping - cleaning up code that's no longer needed.

Why This Matters

Deposits for large contracts can be significant. If you uploaded code for testing or a contract that's been fully migrated away, remove_code gets your deposit back.

Example Scenario

You uploaded contract v1 (code_hash=0xold...). Later, you deployed v2 and migrated all users. No instances of v1 remain. Call remove_code(0xold...). CodeRemoved fires, your deposit returns. 0xold... can no longer be instantiated.

Common Questions

What if instances still exist?
Call fails. You must wait until all contracts using that code are terminated before removal.
Can I remove code I didn't upload?
No. Only the original uploader can remove code. This protects shared code from being pulled out from under active contracts.
What about code used by terminated contracts?
Termination removes the instance's reference. Once all instances are gone (including terminated ones after cleanup), code can be removed.

Use Cases

  • Reclaim deposit from unused code
  • Clean up deprecated contract versions
  • Remove code that was uploaded in error

From Chain Metadata

Remove the code stored under `code_hash` and refund the deposit to its owner. A code can only be removed by its original uploader (its owner) and only if it is not used by any contract.

Input Parameters

#NameTypeDescription
0
code_hash
H256 code_hash (H256)

Permissions

Origin
Unknown
Required Role

Permission data inferred from metadata. May be incomplete.

Requirements

  • Code with given code_hash exists
  • Caller is the original code uploader
  • No contract instances reference this code

Effects

Events Emitted

Storage Modified

Postconditions

  • Code removed from PristineCode
  • CodeInfoOf cleared
  • CodeRemoved event emitted
  • Storage deposit refunded to uploader

Side Effects

  • Code hash no longer available for instantiation
  • Deposit released back to uploader's free balance

Code Examples

import { ApiPromise, WsProvider } from "@polkadot/api";
import { stringCamelCase } from "@polkadot/util";

const provider = new WsProvider("wss://entrypoint-finney.opentensor.ai:443");
const api = await ApiPromise.create({ provider });

// Build remove_code call
const code_hash = 0 as any /* H256 */;

const call = api.tx[stringCamelCase("Contracts")][stringCamelCase("remove_code")](
  code_hash
);

Runtime Info

Pallet Index
29
Call Index
4
First Version
v334
Current Version
v393