{"record":{"id":"806481db7c6295c1","repo":"t8y2/dbx","slug":"the-default-mongodb-id-index-cannot-be-dropped","errorCode":null,"errorMessage":"The default MongoDB _id_ index cannot be dropped","messagePattern":"The default MongoDB _id_ index cannot be dropped","errorType":"validation","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java","lineNumber":1382,"sourceCode":"    static Object parseDropIndexesValue(String indexesJson, boolean single) {\n        if (indexesJson == null || indexesJson.isBlank()) {\n            if (single) {\n                throw new IllegalArgumentException(\"dropIndex requires a string index name or JSON document\");\n            }\n            return \"*\";\n        }\n\n        JsonElement value = JsonParser.parseString(indexesJson);\n        if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {\n            String name = value.getAsString();\n            if (name.isBlank()) {\n                throw new IllegalArgumentException(\"Index name is required\");\n            }\n            if (single && \"*\".equals(name)) {\n                throw new IllegalArgumentException(\"dropIndex does not accept \\\"*\\\"; use dropIndexes() or dropIndexes(\\\"*\\\") instead\");\n            }\n            if (DEFAULT_ID_INDEX_NAME.equals(name)) {\n                throw new IllegalArgumentException(\"The default MongoDB _id_ index cannot be dropped\");\n            }\n            return name;\n        }\n        if (value.isJsonObject()) {\n            JsonObject object = value.getAsJsonObject();\n            if (object.size() == 0) {\n                throw new IllegalArgumentException(\"Index specification is required\");\n            }\n            Document specification = Document.parse(indexesJson);\n            if (isDefaultIdIndexSpecification(specification)) {\n                throw new IllegalArgumentException(\"The default MongoDB _id_ index cannot be dropped\");\n            }\n            return specification;\n        }\n        if (value.isJsonArray()) {\n            if (single) {\n                throw new IllegalArgumentException(\"dropIndex only accepts a string index name or JSON document; arrays are not supported\");\n            }","sourceCodeStart":1364,"sourceCodeEnd":1400,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java#L1364-L1400","documentation":"MongoDB's default _id_ index is created automatically for every collection and cannot be dropped at the server level. The agent rejects requests that name it — either as the literal name \"_id_\" or as an equivalent key-pattern specification — before contacting the server.","triggerScenarios":"Calling dropIndex or dropIndexes with indexes=\"_id_\", or with a JSON document equivalent to the _id index (e.g. {\"_id\": 1}) that matches isDefaultIdIndexSpecification.","commonSituations":"Iterating db.collection.getIndexes() output and dropping every returned index including _id_; trying to rebuild/replace the _id index; cleanup scripts that don't special-case _id_.","solutions":["Skip indexes named \"_id_\" (or key {\"_id\": 1}) in your drop loop","Only drop secondary indexes you explicitly created","To change _id behavior, redesign the schema rather than dropping the index"],"exampleFix":"// before\nfor (Document idx : collection.listIndexes()) {\n    drop(idx.getString(\"name\")); // includes \"_id_\"\n}\n// after\nfor (Document idx : collection.listIndexes()) {\n    String name = idx.getString(\"name\");\n    if (!\"_id_\".equals(name)) {\n        drop(name);\n    }\n}","handlingStrategy":"validation","validationCode":"if (\"_id_\".equals(indexName)) {\n    return; // skip: _id index is mandatory in MongoDB\n}","typeGuard":"boolean isDroppableIndex(Document indexInfo) {\n    String name = indexInfo.getString(\"name\");\n    return name != null && !\"_id_\".equals(name);\n}","tryCatchPattern":"try {\n    agent.execute(\"dropIndex\", params);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"_id_ index cannot be dropped\")) {\n        log.info(\"Skipped mandatory _id index\");\n    } else { throw e; }\n}","preventionTips":["Skip '_id_' when iterating getIndexes()/listIndexes output","Check key patterns containing only {'_id': 1} as well as names","Document that _id indexes are immutable in your maintenance scripts"],"tags":["mongodb","indexes","validation","unsupported-operation"],"backgroundTag":"index-cannot-be-dropped","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}