{"record":{"id":"4461a8b26c1ebf85","repo":"json-path/JsonPath","slug":"invalid-delete-operation","errorCode":null,"errorMessage":"Invalid delete operation","messagePattern":"Invalid delete operation","errorType":"exception","errorClass":"InvalidModificationException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java","lineNumber":119,"sourceCode":"        }\n\n        @Override\n        Object getAccessor() {\n            return \"$\";\n        }\n\n        @Override\n        public void set(Object newVal, Configuration configuration) {\n            throw new InvalidModificationException(\"Invalid set operation\");\n        }\n\n        public void convert(MapFunction mapFunction, Configuration configuration){\n            throw new InvalidModificationException(\"Invalid map operation\");\n        }\n\n        @Override\n        public void delete(Configuration configuration) {\n            throw new InvalidModificationException(\"Invalid delete operation\");\n        }\n\n        @Override\n        public void add(Object newVal, Configuration configuration) {\n            if(configuration.jsonProvider().isArray(parent)){\n                configuration.jsonProvider().setArrayIndex(parent, configuration.jsonProvider().length(parent), newVal);\n            } else {\n                throw new InvalidModificationException(\"Invalid add operation. $ is not an array\");\n            }\n        }\n\n        @Override\n        public void put(String key, Object newVal, Configuration configuration) {\n            if(configuration.jsonProvider().isMap(parent)){\n                configuration.jsonProvider().setProperty(parent, key, newVal);\n            } else {\n                throw new InvalidModificationException(\"Invalid put operation. $ is not a map\");\n            }","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java#L101-L137","documentation":"The root ('$') PathRef rejects delete: removing the entire document root is not a meaningful modification, so delete(Configuration) throws InvalidModificationException 'Invalid delete operation' unconditionally when the deletion path resolves to the root.","triggerScenarios":"Calling JsonPath.delete(document, \"$\", configuration) — using the root path as delete target.","commonSituations":"Bulk cleanup code that deletes every path in a collected list where one entry is the root; misunderstanding that delete removes children of the root rather than the root itself.","solutions":["Delete specific sub-paths (e.g. \"$.field\" or \"$.array[0]\") instead of \"$\".","To clear the document, replace your document reference with null or an empty object outside JsonPath.","Skip paths equal to \"$\" when iterating over paths selected for deletion.","Catch InvalidModificationException if root deletion may occur and handle it explicitly."],"exampleFix":"// before\nJsonPath.delete(document, \"$\", conf); // throws\n// after\nJsonPath.delete(document, \"$.unwantedField\", conf);","handlingStrategy":"validation","validationCode":"if (\"$\".equals(path)) {\n    throw new IllegalArgumentException(\"Cannot JsonPath.delete the root; null/replace the document reference instead\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    JsonPath.delete(document, path, conf);\n} catch (InvalidModificationException e) {\n    throw new IllegalStateException(\"Delete at path '\" + path + \"' is not supported (root cannot be deleted)\", e);\n}","preventionTips":["Delete concrete sub-paths only, never \"$\"","Special-case or skip root entries in bulk-delete loops","To clear the document, reassign the variable to null or an empty object","Catch InvalidModificationException when paths are user-supplied"],"tags":["json-path","delete-operation","root-path","invalid-modification"],"backgroundTag":"unsupported-operation","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}