Dfx Error: Canister cannot be installed because the canister is not empty

I’m a new developer struggling with dfx. This is one of the problems I’m running into when doing “Hakin’ on The Internet Computer ep.1.” I’m using a linux machine and it should already have all the recommended packages installed.

Here’s the command line process and where I get stuck:

dfx new phonebook
-The canister files were never made and errors with dfx build lead me to this
dfx canister create phonebook_assets
dfx canister create phonebook
dfx canister install phonebook
dfx canister install --all
-The Replica returned an error: code 5, message: “Canister X cannot be installed because the canister is not empty. Try installing with mode=‘reinstall’ instead.”

Why does this happen, will this repeat with every change to the main.mo file, and how do I fix it?

By the way, the canister does work when I call it;
dfx canister call phonebook greet world
(“Hello, world!”)
But I can’t get any updates to the code to take.

Any help much appreciated.

1 Like

try

dfx deploy

or

dfx canister install --all --mode upgrade

or
dfx canister install --all --mode=upgrade

can’t remember if you need the =

2 Likes

dfx canister install --all --mode reinstall

5 Likes

Looks like you might be missing this step:
dfx build

You also might want to check out sdk.dfinity.org/docs/quickstart/quickstart-local.html

Thanks. I ran into the same “not empty” issue and this command fixed it.

1 Like