{"record":{"id":"e1bafa17c8a66f84","repo":"apache/cassandra","slug":"attempted-to-delete-an-element-from-a-list-which-i","errorCode":null,"errorMessage":"Attempted to delete an element from a list which is null","messagePattern":"Attempted to delete an element from a list which is null","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/terms/Lists.java","lineNumber":563,"sourceCode":"\n        public void execute(DecoratedKey partitionKey, RowUpdateBuilder builder) throws InvalidRequestException\n        {\n            assert column.type.isMultiCell() : \"Attempted to delete an item by index from a frozen list\";\n\n            Guardrails.readBeforeWriteListOperationsEnabled\n            .ensureEnabled(\"Removal of list items by index requiring read before write\", builder.clientState);\n\n            Term.Terminal index = t.bind(builder);\n            if (index == null)\n                throw new InvalidRequestException(\"Invalid null value for list index\");\n            if (index == Constants.UNSET_VALUE)\n                return;\n\n            Row existingRow = builder.getPrefetchedRow(partitionKey, builder.currentClustering());\n            int existingSize = existingSize(existingRow, column);\n            int idx = ByteBufferUtil.toInt(index.get());\n            if (existingSize == 0)\n                throw new InvalidRequestException(\"Attempted to delete an element from a list which is null\");\n            if (idx < 0 || idx >= existingSize)\n                throw new InvalidRequestException(String.format(\"List index %d out of bound, list has size %d\", idx, existingSize));\n\n            builder.addTombstone(column, existingRow.getComplexColumnData(column).getCellByIndex(idx).path());\n        }\n    }\n}\n","sourceCodeStart":545,"sourceCodeEnd":571,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/terms/Lists.java#L545-L571","documentation":"Thrown when deleting a list element by index on a row whose list column is null or empty (no existing collection data). Since there is no element to remove, Cassandra rejects the delete rather than silently no-op-ing.","triggerScenarios":"`DELETE l[?] FROM t WHERE ...` where the target row has no list value (list is null) or exists with size 0, and the index binds to a valid non-null value.","commonSituations":"Deleting by index on a row that was never written with a list; rows where the list was previously set to null; concurrent deletes that emptied the list between read and write.","solutions":["Check that the row exists and the list is non-empty before issuing an index-based delete.","Set the list first (e.g. `SET l = ?`) or use `l = l - ?` element-based removal which tolerates missing elements.","Handle InvalidRequestException for this case and treat it as a no-op in application logic."],"exampleFix":"// before\nsession.execute(\"DELETE l[?] FROM t WHERE k=?\", idx, key);\n// after\nRow row = session.execute(\"SELECT l FROM t WHERE k=?\", key).one();\nif (row != null && !row.isNull(\"l\"))\n    session.execute(\"DELETE l[?] FROM t WHERE k=?\", idx, key);","handlingStrategy":"validation","validationCode":"Row r = session.execute(\"SELECT l FROM t WHERE k=?\", key).one();\nif (r == null || r.isNull(\"l\")) return; // nothing to delete","typeGuard":"boolean listExists(Row r, String col) { return r != null && !r.isNull(col); }","tryCatchPattern":"try { session.execute(\"DELETE l[?] FROM t WHERE k=?\", idx, key); } catch (InvalidRequestException e) { if (e.getMessage().contains(\"list which is null\")) { /* treat as no-op */ } else throw e; }","preventionTips":["Ensure the row/list exists before index-based deletes","Prefer element-value removal (l = l - [v]) which tolerates missing data","Re-read the row in the same logical transaction as the delete"],"tags":["cql","list","empty-collection","invalid-request"],"backgroundTag":"empty-required-field","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}