{"record":{"id":"3459ff269c9ca535","repo":"hyperledger/fabric","slug":"chaincode-s-is-already-instantiated-3459ff","errorCode":null,"errorMessage":"Chaincode %s is already instantiated","messagePattern":"Chaincode (.+?) is already instantiated","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/handlers/validation/builtin/v13/lscc_validation_logic.go","lineNumber":497,"sourceCode":"\t\t\treturn policyErr(fmt.Errorf(\"expected cc version %s, found %s\", cdsArgs.ChaincodeSpec.ChaincodeId.Version, cdRWSet.Version))\n\t\t}\n\t\t// it must only write to 2 namespaces: LSCC's and the cc that we are deploying/upgrading\n\t\tfor _, ns := range txRWSet.NsRwSets {\n\t\t\tif ns.NameSpace != \"lscc\" && ns.NameSpace != cdRWSet.Name && len(ns.KvRwSet.Writes) > 0 {\n\t\t\t\treturn policyErr(fmt.Errorf(\"LSCC invocation is attempting to write to namespace %s\", ns.NameSpace))\n\t\t\t}\n\t\t}\n\n\t\tlogger.Debugf(\"Validating %s for cc %s version %s\", lsccFunc, cdRWSet.Name, cdRWSet.Version)\n\n\t\tswitch lsccFunc {\n\t\tcase lscc.DEPLOY:\n\n\t\t\t/******************************************************************/\n\t\t\t/* security check 1 - cc not in the LCCC table of instantiated cc */\n\t\t\t/******************************************************************/\n\t\t\tif ccExistsOnLedger {\n\t\t\t\treturn policyErr(fmt.Errorf(\"Chaincode %s is already instantiated\", cdsArgs.ChaincodeSpec.ChaincodeId.Name))\n\t\t\t}\n\n\t\t\t/****************************************************************************/\n\t\t\t/* security check 2 - validation of rwset (and of collections if enabled) */\n\t\t\t/****************************************************************************/\n\t\t\tif ac.PrivateChannelData() {\n\t\t\t\t// do extra validation for collections\n\t\t\t\terr := vscc.validateRWSetAndCollection(lsccrwset, cdRWSet, lsccArgs, lsccFunc, ac, chid)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// there can only be a single ledger write\n\t\t\t\tif len(lsccrwset.Writes) != 1 {\n\t\t\t\t\treturn policyErr(fmt.Errorf(\"LSCC can only issue a single putState upon deploy\"))\n\t\t\t\t}\n\t\t\t}\n","sourceCodeStart":479,"sourceCodeEnd":515,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/handlers/validation/builtin/v13/lscc_validation_logic.go#L479-L515","documentation":"On the lscc DEPLOY path, the validator checks whether the chaincode already exists on the ledger (ccExistsOnLedger). If it does, deploying it again is rejected because a chaincode with the same name can only be instantiated once per channel. Upgrades, not re-deploys, are the supported mechanism.","triggerScenarios":"Issuing an lscc deploy (instantiation) for a chaincode name that already has a ChaincodeData record on the ledger — e.g. running instantiate twice, or re-running a deploy script after a partially failed upgrade.","commonSituations":"Retry logic re-submitting an instantiate proposal that actually succeeded, deployment automation without idempotency checks, attempting to change an already-instantiated chaincode via deploy instead of upgrade, restoring a ledger backup and replaying deploys.","solutions":["Use upgrade instead of deploy if you want to change the chaincode version or policy.","Query lscc (getchaincodes) or the ledger first to confirm the chaincode is not already instantiated before deploying.","Make deployment scripts idempotent — skip deploy when the chaincode already exists on the channel.","If a prior transaction actually committed, do not re-submit; check transaction status by txid."],"exampleFix":"// before\nclient.invoke(new InstantiateProposalRequest...) // every run\n// after\nif (!lscc.getChaincodes().contains(\"mycc\")) { client.invoke(instantiateReq); } else { client.invoke(upgradeReq); }","handlingStrategy":"validation","validationCode":"// check before instantiating\nresp, _ := lsccClient.Query(\"getchaincodes\")\nif chaincodeListContains(resp, \"mycc\") {\n    return errors.New(\"mycc already instantiated; use upgrade instead of deploy\")\n}","typeGuard":"func isInstantiated(codes []lscc.ChaincodeQueryResponse, name string) bool {\n    for _, c := range codes { if c.Name == name { return true } }\n    return false\n}","tryCatchPattern":"catch (err) { if (String(err).includes('already instantiated')) { /* switch to an upgrade request or treat as success if idempotent retry */ } }","preventionTips":["Make deploy scripts idempotent via getchaincodes checks","Track txids so successful transactions are not resubmitted","Always upgrade rather than re-deploy for changes"],"tags":["hyperledger-fabric","lscc","chaincode-deploy","duplicate-instantiation"],"backgroundTag":"chaincode-already-instantiated","analyzedSha":"2736b63f8fd5932511d56fe68b7039d15977f7f6","analyzedAt":"2026-09-04T08:52:36.465Z","contentChangedAt":"2026-09-04T08:52:36.465Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}