karatelabs/karate · error · RuntimeException

remove() needs two arguments: variable name and path

Error message

remove() needs two arguments: variable name and path

What it means

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.

Solutions

  1. Supply both arguments: karate.remove('myVar', '$.foo') for JSON or karate.remove('myXml', '/root/node') for XML.
  2. If you intended to drop the whole variable, set it to null instead of calling remove().
  3. Guard dynamic path variables: ensure the path string is defined and non-empty before the call.

Example fix

// before
karate.remove('myJson')

// after
karate.remove('myJson', '$.unwantedField')
Defensive patterns

Strategy: validation

Validate before calling

// JS, before calling
if (typeof varName !== 'string' || typeof path !== 'string' || path.length === 0) {
  throw new Error('remove() requires a variable name and a non-empty path');
}
karate.remove(varName, path);

Type guard

function canRemove(name, path) { return typeof name === 'string' && typeof path === 'string' && path.length > 0; }

Try / catch

try {
  karate.remove('myJson', path);
} catch (e) {
  if (String(e.message).indexOf('remove() needs two arguments') !== -1) {
    throw new Error('remove() called without a name+path — path resolved to: ' + path);
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/f2313b6a62019d4a. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJs.java:759

     * karate.expect(actual).to.equal(expected)
     * karate.expect(actual).to.be.a('string')
     * karate.expect(actual).to.have.property('name')
     * karate.expect(actual).to.not.equal(unexpected)
     * </pre>
     */
    private JavaCallable expect() {
        return (context, args) -> {
            if (args.length == 0) {
                throw new RuntimeException("expect() needs at least one argument");
            }
            return new Expect(args[0], onMatch);
        };
    }

    private JavaInvokable remove() {
        return args -> {
            if (args.length < 2) {
                throw new RuntimeException("remove() needs two arguments: variable name and path");
            }
            String varName = args[0].toString();
            String path = args[1].toString();
            Object var = engine.get(varName);
            if (var instanceof Node && path != null && path.startsWith("/")) {
                // XPath remove on XML
                Document doc = var instanceof Document ? (Document) var : ((Node) var).getOwnerDocument();
                Xml.removeByPath(doc, path);
            } else if ((var instanceof Map || var instanceof List) && path != null) {
                // Route through Json so nested paths (props.field3), JsonPath ($.props.field3)
                // and bracketed keys all resolve - mirroring the `remove` keyword. A bare
                // top-level key is normalized to a JsonPath by Json.prefix().
                Json.of(var).remove(path);
            }
            return null;
        };
    }

View on GitHub (pinned to a22eb90246)