Developer Resources

Managing Canisters

Learn everything you need to know about managing canister smart contracts on the Internet Computer

IC Academy » Developer Resources » Managing Canisters

Manage Canister Smart Contracts

Managing canisters throughout the entire canister lifecycle

Obtaining canister identifier

Depending on your preferences, you can obtain a canister smart contract identifier either before or after you have a program to compile.

To reserve a unique identifier on a subnet before writing code, run the following command:

Copy to Clipboard

This will create an empty canister placeholder. Later on, you can install your code into the empty placeholder.

These are the steps to obtain a unique identifier for your canister:

#1 – New terminal window

Open new terminal window or a new tab on your computer

#2 – Create new project

To create a new project for the canister you want to create, run the following command (replace PROJECT-NAME with the name of your project which is by default used as the canister name as well):

Copy to Clipboard
#3 – Change to new project directory

Once you have run the command, you can change to the new project directory.

#4 – Setting the host and port Only for network deployment

To set the host and port for the execution environment of your canister (e.g. the Internet Computer blockchain), open the dfx.json file.

You can skip this step if you are using a local deployment on your computer.

At this stage, you can optionally make changes to the names of your canisters. You can also add canisters settings to your configuration file should you wish to create new identifiers for additional canisters before compiling code.

#5 – Start local execution environment

You may be able to skip step #5 if you are not running your canisters locally.

If necessary, start local canister execution environment via:

Copy to Clipboard

Be sure to include the --network command-line option if you want to run your canisters on a remote execution environment such as the Internet Computer blockchain. Adding this option ensures that tasks can be performed in the specified environment.

The command should look as follows:

Copy to Clipboard
#6 – Registering unique identifiers

In the last step, you can register unique identifiers for the canisters you have defined in the dfx.json file. To do so, run the following command:

Copy to Clipboard

Running this command creates the .dfx/local directory. Within the directory, the canister_ids.json file is being created.

Building canister with local identifier

Before deploying your source code as a canister, be sure to compile your code into a WebAssembly module. You can generate an identifier for your project that is locally defined in case you are just compiling the project for local debugging purposes.

Follow these steps to generate the locally-defined identifier:

#1 – Create project

Create your project with the configuration and program logic you need.

#2 – Start local execution environment

If necessary, start the local canister execution environment via:

Copy to Clipboard

For a remote execution environment such as the Internet Computer blockchain, add --network

The command should look as follows:

Copy to Clipboard
#3 – Generate hard-coded local identifiers

In the last step, run the following command to generate hard-coded local identifiers for all your canisters defined in dfx.json:

Copy to Clipboard

Keep in mind to replace locally-defined identifiers by registering unique identifiers before deploying on the IC.

Deploying canister

Once the program is compiled, you can install it in a canister smart contract. You can choose between running the code in a local execution environment or on the IC blockchain.

For a walk-through on how to deploy a canister with cycles, have a look at Deploying Canisters.

Follow these steps to deploy your code:

#1 – Open new terminal

Navigate to the directory of your project by opening a new terminal.

#2 – Start local execution environment

If necessary, start the local canister execution environment via:

Copy to Clipboard

For a remote execution environment such as the Internet Computer blockchain, add --network

The command should look as follows:

Copy to Clipboard
#3 – ID verification

Before deploying, make sure to verify that all canisters you wish to deploy have canister identifiers.

#4 – Deploy

Deploy all canisters via:

Copy to Clipboard

Looking up canister identifier

Each canister has a unique identifier. To interact with a canister, you typically need its identifier. The files used for storing the information are located in different directories as the identifiers are specific to the deployment environment.

You can find the identifiers for locally deployed canisters in the following file of the project .dfx/local/canister_ids.json.

Run the dfx canister id command to look up the identifier of a specific canister.

Looking up identifier on local execution environment
Copy to Clipboard
Looking up identifier on IC environment
Copy to Clipboard

Adding wallet for existing canister

When creating a new project in a local development, a default wallet is automatically created. If you have already created a canister in a network deployment, you can force the creation of a wallet with the following steps:

#1 – Navigate to project directory

Open your terminal and navigate to the directory of your project

#2 – Stop local execution environment

If necessary, run the following command to stop the local canister execution environment:

Copy to Clipboard
#3 – Delete .dfx directory
#4 – Start local execution environment

Once the .dfx directory is deleted, run the following command to start the local execution environment:

Copy to Clipboard

Reinstalling canister

The process of debugging may require you to install and replace your program during the development cycle. In such a case, you may wish to delete the canister’s code or state while keeping the canister identifier.

To reinstall a canister, follow these steps:

#1 – Navigate to project directory

Open your terminal and navigate to the directory of your project

#2 – Start local execution environment

If necessary, start the local canister execution environment via:

Copy to Clipboard

For a remote execution environment such as the Internet Computer blockchain, add --network

The command should look as follows:

Copy to Clipboard
#3 – ID verification

Before re-deploying, make sure to verify that all canisters you wish to deploy have canister identifiers.

#4 – Re-deploy

Re-deploy all canisters via:

Copy to Clipboard

Use reinstall to replace any canister. This mode will work independently of whether or not the canister has code or a state associated with it.

Setting identity as canister owner

If you run the dfx canister create command for the first time, the system automatically creates a default user identity – in most cases. As a default, the identity contains both the public and private key pair for your local user account. If you create projects and deploy canisters, the default identity will also be set as the default owner of these. To circumvent this, you can proactively create identities of your own choice.

To illustrate how this works, we will be creating the identity for registered_owner that is used to register, create, deploy, and call the pubs project. Here’s how you can set an identity for your project:

#1 – Create new project via the following command
Copy to Clipboard
#2 – Switch to the project directory
Copy to Clipboard
#3 – Start local canister execution environment
Copy to Clipboard
#4 – Create new identity
Copy to Clipboard
#5 – Set active user context to use the new identity
Copy to Clipboard
#6 – Register, build, and deploy canisters
Copy to Clipboard

You’ve set the active user context to use the registered_owner in step #5. Therefore, these commands are using this new identity, making it the owner of the deployed canisters.

#7 – Verify successful deployment

Lastly, verify if the canister has been successfully deployed by calling the greet function.

Copy to Clipboard

Managing running state of canister

A canister is in a Running state if it is available to send requests and to receive replies.

By default, canisters are placed in the Running state. But there may be cases in which you may want to temporarily or even permanently stop your canister. Such cases include:

  • Stopping the canister before you upgrade it
  • Ensuring proper handling of messages that are in progress. Messages will either run to completion or are rolled back
  • Clearing a canister’s message queue cleanly before deleting it
Checking the current status of all canisters in local execution environment
Copy to Clipboard

Running the command returns output similar to this (if there are canisters currently running):

Copy to Clipboard
Stopping currently running canisters
Copy to Clipboard

Running the command returns output similar to the following:

Copy to Clipboard
Restarting a canister
Copy to Clipboard

Running the command returns output similar to the following:

Copy to Clipboard

Upgrading a canister

  • Reinstalling a canister preserves its identifier but not its state
  • Upgrading a canister preserves the state and allows you to change the code.

Use the stable keyword to preserve state when upgrading a canister written in Motoko. Doing so helps you to identify variables you wish to preserve.

Follow these steps to upgrade your canister:

#1 – Navigate to project directory

Open your terminal and navigate to the directory of your project

#2 – Start local execution environment

If necessary, start the local canister execution environment via:

Copy to Clipboard

For a remote execution environment such as the Internet Computer blockchain, add --network

The command should look as follows:

Copy to Clipboard
#3 – Verify canister identifiers

Make sure to verify all the canisters you want to upgrade have identifiers. In the variable declaration, use the stable keyword to help your program identify variables for which to maintain state.

#4 – Upgrade all canisters
Copy to Clipboard

Deleting canister

The dfx canister delete command enables you to permanently delete canisters. You can decide if you want to delete only one specific canister or all canisters of a specific project running on a given deployment.

Before deleting a canister, you must stop the canister to clear pending message requests or pending replies. If you delete a canister, the canister identifier, its state, and its code will be removed.

Follow these steps to delete all the canisters of your project:

#1 – Navigate to project directory

Open your terminal and navigate to the directory of your project

#2 – Start local execution environment

If necessary, start the local canister execution environment via:

Copy to Clipboard

For a remote execution environment such as the Internet Computer blockchain, add --network

The command should look as follows:

Copy to Clipboard
#3 – Check status of project canisters running in local execution environment
Copy to Clipboard
#4 – Stop all project canisters
Copy to Clipboard
#5 – Delete all project canisters
Copy to Clipboard

What Are Cycles?

Cycles are the Internet Computer’s computational resource that powers canisters

More about Cycles

Creating Cycles

Learn how you can create cycles to power your canister smart contracts

Learn More