{"record":{"id":"c49e39afffc5d4fc","repo":"mikefarah/yq","slug":"v-v-cannot-be-subtracted-from-v-c49e39","errorCode":null,"errorMessage":"%v (%v) cannot be subtracted from %v","messagePattern":"(.+?) \\((.+?)\\) cannot be subtracted from (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/operator_subtract.go","lineNumber":55,"sourceCode":"\t\t\tnewLHSArray = append(newLHSArray, lhs.Content[lindex])\n\t\t}\n\t}\n\treturn newLHSArray\n}\n\nfunc subtract(_ *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {\n\tif lhs.Tag == \"!!null\" {\n\t\treturn lhs.CopyAsReplacement(rhs), nil\n\t}\n\n\ttarget := lhs.CopyWithoutContent()\n\n\tswitch lhs.Kind {\n\tcase MappingNode:\n\t\treturn nil, fmt.Errorf(\"maps not yet supported for subtraction\")\n\tcase SequenceNode:\n\t\tif rhs.Kind != SequenceNode {\n\t\t\treturn nil, fmt.Errorf(\"%v (%v) cannot be subtracted from %v\", rhs.Tag, rhs.GetNicePath(), lhs.Tag)\n\t\t}\n\t\ttarget.Content = subtractArray(lhs, rhs)\n\tcase ScalarNode:\n\t\tif rhs.Kind != ScalarNode {\n\t\t\treturn nil, fmt.Errorf(\"%v (%v) cannot be subtracted from %v\", rhs.Tag, rhs.GetNicePath(), lhs.Tag)\n\t\t}\n\t\ttarget.Kind = ScalarNode\n\t\ttarget.Style = lhs.Style\n\t\tif err := subtractScalars(context, target, lhs, rhs); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\n\treturn target, nil\n}\n\nfunc subtractScalars(context Context, target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) error {\n\tlhsTag := lhs.Tag","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_subtract.go#L37-L73","documentation":"yq's subtract operator (`-`) only supports sequence - sequence (array difference), scalar - scalar, and null - anything. This error is thrown when the LEFT operand is an array (SequenceNode) but the RIGHT operand is not an array (e.g. a scalar or map), because there is no defined way to 'subtract' a non-array from an array. The message reports the offending rhs tag, its nice path, and the lhs tag.","triggerScenarios":"Running a yq expression like `[1,2,3] - 2`, `.a[] - .b` where .a is a sequence and .b is a scalar/map, or `.myList - .notAList` in any yq invocation (CLI or Go API via subtractOperator).","commonSituations":"Config transform scripts where the user assumes `-` removes a single element from an array; a YAML key that changed from scalar to list (or vice versa) upstream so the expression now mixes kinds; copy-pasted jq-style expressions assuming scalar set membership removal.","solutions":["Wrap the right operand in an array so both sides are sequences: `.myList - [.removedItem]`","Check the kind of both operands with `type` first (e.g. `select(type == \"!!seq\")`) and fix the data or the expression","If you meant element-wise numeric subtraction, use map over elements instead: `.myList | map(. - 2)`","If the right value should be a list, fix the input document or select the correct key"],"exampleFix":"// before: yq '.items - 3' file.yml  -> error\n// after\nyq '.items - [3]' file.yml","handlingStrategy":"validation","validationCode":"// shell check before running the expression\nlhs_type=$(yq '.lhs | type' file.yml)\nrhs_type=$(yq '.rhs | type' file.yml)\n[ \"$lhs_type\" = \"!!seq\" ] && [ \"$rhs_type\" = \"!!seq\" ] || { echo \"subtract needs seq-seq, got $lhs_type - $rhs_type\"; exit 1; }","typeGuard":"def is_seq(node):\n    return node.get('kind') == 'sequence'  # or use yq 'type' == '!!seq'","tryCatchPattern":"null","preventionTips":["Always wrap single-element removals in an array: `.list - [item]`","Check operand kinds with `type` when expression inputs come from varying documents","Prefer `map(. - n)` for element-wise numeric ops on arrays"],"tags":["yq","type-mismatch","subtract-operator","arrays"],"backgroundTag":"operand-type-mismatch","analyzedSha":"8b5af0694bb82b41d4ae180fac9972029066f90a","analyzedAt":"2026-09-05T10:57:22.766Z","contentChangedAt":"2026-09-05T10:57:22.766Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}