{"record":{"id":"c1fad00e331d8340","repo":"hyperledger/fabric","slug":"chaincode-s-is-already-instantiated","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/v12/validation_logic.go","lineNumber":639,"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":621,"sourceCodeEnd":657,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/handlers/validation/builtin/v12/validation_logic.go#L621-L657","documentation":"For lscc DEPLOY, the validator checks that the target chaincode does not already exist on the ledger (ccExistsOnLedger). If a ChaincodeData entry for the name already exists, a second 'deploy' is rejected — upgrades must use the UPGRADE function instead.","triggerScenarios":"Invoking lscc with function 'deploy' (SDK 'instantiate') for a chaincode name that is already instantiated on the channel, instead of 'upgrade'.","commonSituations":"Re-running instantiate on an already-deployed chaincode; forgetting to switch the SDK call from instantiateChaincode to upgradeChaincode when adding a new version; duplicate chaincode names across teams on one channel.","solutions":["Use the upgrade flow (lscc 'upgrade' / SDK upgradeChaincode) to deploy a new version of the existing chaincode.","Choose a unique chaincode name if this is genuinely a new deployment.","Query the ledger (e.g. chaincode list --instantiated) before deploying to check for an existing entry.","Clean up test channels or use distinct names per environment to avoid collisions."],"exampleFix":"// before\nclient.instantiateChaincode({ chaincodeId: 'mycc', chaincodeVersion: '2.0' })\n// after\nclient.upgradeChaincode({ chaincodeId: 'mycc', chaincodeVersion: '2.0' })","handlingStrategy":"validation","validationCode":"// client-side: check before instantiating\nresp, _ := admin.QueryInstantiatedChaincodes(channel)\nfor _, cc := range resp.Chaincodes {\n    if cc.Name == chaincodeID {\n        return fmt.Errorf(\"%s already instantiated; use upgrade instead\", chaincodeID)\n    }\n}","typeGuard":"func alreadyInstantiated(instantiated []*pb.ChaincodeQueryResponse_ChaincodeQueryResponseInfo, name string) bool {\n    for _, cc := range instantiated {\n        if cc.Name == name { return true }\n    }\n    return false\n}","tryCatchPattern":"if err := submitTx(envelope); err != nil {\n    if strings.Contains(err.Error(), \"is already instantiated\") {\n        // switch to upgradeChaincode with a new version, or pick a new chaincode name\n    }\n}","preventionTips":["Query instantiated chaincodes before deploying","Use upgradeChaincode for new versions of existing chaincode","Adopt unique chaincode naming conventions per team/channel"],"tags":["hyperledger-fabric","lscc","instantiate","duplicate"],"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"}