{"record":{"id":"17115f0064e86ae7","repo":"1Panel-dev/1Panel","slug":"failed-to-drop-database-dbname","errorCode":null,"errorMessage":"failed to drop database ${dbName}","messagePattern":"failed to drop database (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"agent/app/service/database_mongodb.go","lineNumber":443,"sourceCode":"});\n`, dbNameJSON, usernameJSON, passwordJSON, permissionJSON)), nil\n}\n\nfunc buildMongodbDeleteScript(dbName string) (string, error) {\n\tdbNameJSON, err := json.Marshal(dbName)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\treturn strings.TrimSpace(fmt.Sprintf(`\nconst dbName = %s;\nconst targetDb = db.getSiblingDB(dbName);\nconst dropUsersResult = targetDb.runCommand({ dropAllUsersFromDatabase: 1 });\nif (!dropUsersResult || dropUsersResult.ok !== 1) {\n  throw new Error(\"failed to drop users from \" + dbName);\n}\nconst dropDatabaseResult = targetDb.runCommand({ dropDatabase: 1 });\nif (!dropDatabaseResult || dropDatabaseResult.ok !== 1) {\n  throw new Error(\"failed to drop database \" + dbName);\n}\n`, dbNameJSON)), nil\n}\n\nfunc buildMongodbBindUserScript(dbName, username, password string) (string, error) {\n\tdbNameJSON, err := json.Marshal(dbName)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tusernameJSON, err := json.Marshal(username)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tpasswordJSON, err := json.Marshal(password)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\treturn strings.TrimSpace(fmt.Sprintf(`","sourceCodeStart":425,"sourceCodeEnd":461,"githubUrl":"https://github.com/1Panel-dev/1Panel/blob/5ac7c808815b0691009cd390414f03471312262f/agent/app/service/database_mongodb.go#L425-L461","documentation":"Same generated delete script as error 0: after users are dropped, `targetDb.runCommand({ dropDatabase: 1 })` returned `ok !== 1`. In stock MongoDB `dropDatabase` on a non-existent database still returns ok:1, so this failure is a privilege/authentication problem, not a missing-db problem. The error text is produced inside the mongosh eval and propagated as a non-zero docker exec by runMongodbAdminScript (database_mongodb.go:357).","triggerScenarios":"Deleting a MongoDB database when the mongosh URI user lacks the `dbAdmin`/`root` role covering the target db; the container's stored password no longer matches; or a replica-set node that is not primary returns 'not primary' via runCommand (ok:0).","commonSituations":"Non-root user configured at install time; root password rotated outside 1Panel; MongoDB running as a replica set where the exec'd mongosh connects to a secondary; database name containing characters the URI/JS marshaling mangles.","solutions":["Inspect the raw failure: run the same script manually in the container and read `codeName`/`errmsg` from the dropDatabase result","If 'not primary', point the exec at the primary (or step down/fix the set) and retry","Ensure the stored app-install credential has root or dbAdminAnyDatabase on the auth db","Retry the 1Panel delete after credentials/privileges are fixed — the preceding dropAllUsersFromDatabase step is idempotent"],"exampleFix":"// manual diagnosis inside the container\n// mongosh -u root -p <pass> --authenticationDatabase admin --eval \\\n//   'const r = db.getSiblingDB(\"mydb\").runCommand({dropDatabase:1}); printjson(r)'","handlingStrategy":"try-catch","validationCode":"// ensure the credential can drop on this db before the flow\n// db.getSiblingDB(dbName).runCommand({dropDatabase:1}) is itself the validation; guard instead on roles:\n// require role root or dbAdminAnyDatabase in connectionStatus","typeGuard":null,"tryCatchPattern":"// delete is idempotent (users drop + db drop); safe to retry once after credential fix\nif err := deleteFlow(); err != nil {\n    if isAuthRelated(err) { fixCredential(); err = deleteFlow() }\n    if err != nil { return fmt.Errorf(\"drop database %s: %w\", dbName, err) }\n}","preventionTips":["For replica sets, run deletes against the primary","Monitor credential drift: compare stored password with a periodic auth probe","Treat any ok!==1 as a server-side codeName — always capture docker logs on failure"],"tags":["mongodb","mongosh","authorization","replica-set"],"backgroundTag":null,"analyzedSha":"5ac7c808815b0691009cd390414f03471312262f","analyzedAt":"2026-08-15T14:02:06.953Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}