{"record":{"id":"f2313b6a62019d4a","repo":"karatelabs/karate","slug":"remove-needs-two-arguments-variable-name-and-path","errorCode":null,"errorMessage":"remove() needs two arguments: variable name and path","messagePattern":"remove\\(\\) needs two arguments: variable name and path","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJs.java","lineNumber":759,"sourceCode":"     * karate.expect(actual).to.equal(expected)\n     * karate.expect(actual).to.be.a('string')\n     * karate.expect(actual).to.have.property('name')\n     * karate.expect(actual).to.not.equal(unexpected)\n     * </pre>\n     */\n    private JavaCallable expect() {\n        return (context, args) -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"expect() needs at least one argument\");\n            }\n            return new Expect(args[0], onMatch);\n        };\n    }\n\n    private JavaInvokable remove() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"remove() needs two arguments: variable name and path\");\n            }\n            String varName = args[0].toString();\n            String path = args[1].toString();\n            Object var = engine.get(varName);\n            if (var instanceof Node && path != null && path.startsWith(\"/\")) {\n                // XPath remove on XML\n                Document doc = var instanceof Document ? (Document) var : ((Node) var).getOwnerDocument();\n                Xml.removeByPath(doc, path);\n            } else if ((var instanceof Map || var instanceof List) && path != null) {\n                // Route through Json so nested paths (props.field3), JsonPath ($.props.field3)\n                // and bracketed keys all resolve - mirroring the `remove` keyword. A bare\n                // top-level key is normalized to a JsonPath by Json.prefix().\n                Json.of(var).remove(path);\n            }\n            return null;\n        };\n    }\n","sourceCodeStart":741,"sourceCodeEnd":777,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L741-L777","documentation":"karate.remove(varName, path) deletes a value from a variable at the given JSON path or XPath (path starting with '/' targets XML). The library throws this error when fewer than two arguments are supplied, because both the target variable name and the path are mandatory to locate what to remove.","triggerScenarios":"Calling karate.remove(name) with only the variable name, or karate.remove() with none — usually from passing an undefined second variable or misusing the one-argument form of a different karate API.","commonSituations":"Removing a JSON key but forgetting the path argument; assuming remove(var) deletes the whole variable (it does not — use karate.set(name, null) or similar); dynamic paths that evaluate to undefined.","solutions":["Supply both arguments: karate.remove('myVar', '$.foo') for JSON or karate.remove('myXml', '/root/node') for XML.","If you intended to drop the whole variable, set it to null instead of calling remove().","Guard dynamic path variables: ensure the path string is defined and non-empty before the call."],"exampleFix":"// before\nkarate.remove('myJson')\n\n// after\nkarate.remove('myJson', '$.unwantedField')","handlingStrategy":"validation","validationCode":"// JS, before calling\nif (typeof varName !== 'string' || typeof path !== 'string' || path.length === 0) {\n  throw new Error('remove() requires a variable name and a non-empty path');\n}\nkarate.remove(varName, path);","typeGuard":"function canRemove(name, path) { return typeof name === 'string' && typeof path === 'string' && path.length > 0; }","tryCatchPattern":"try {\n  karate.remove('myJson', path);\n} catch (e) {\n  if (String(e.message).indexOf('remove() needs two arguments') !== -1) {\n    throw new Error('remove() called without a name+path — path resolved to: ' + path);\n  }\n  throw e;\n}","preventionTips":["Always pass both name and path; remember remove() never deletes the whole variable.","Use '$.field' JSON paths for JSON vars and '/root/node' XPath for XML vars.","Validate dynamically computed paths are non-empty strings before the call."],"tags":["json","xml","argument-count","karate"],"backgroundTag":"missing-required-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}