#3538·jq

Inconsistent delpaths behavior with negative indices

Author: cscottCreated May 4, 2026Updated Jul 4, 2026
Labelsbug

Describe the bug There is attempt to reconcile positive and negative indices and execute all deletions as if they were simultaneous, as demonstrated in jq.test around line 1184:

del(.[1], .[-6], .[2], .[-3:9])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 3, 5, 6, 9]

but this only reconciles positive and negative indices when they occur in the leaves of the path. Negative indices in non-leaf nodes are not collected together and so we get different results -- as if all the deletions corresponding to negative indices are evaluated first, and then the deletions on positive indices are run on the shortened array (altering where they apply).

To Reproduce Here are some examples:

On a single-level array, the results are as if the 6 and -6 were evaluated simultaneously, and the elements 4 and 6 are deleted.

$ echo '[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]' | jq -c 'del(.[-6],.[6])'
[0,1,2,3,5,7,8,9]

And on a two-level array with all positive or all negative indices, we get the same behavior:

$ echo '[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]' | jq -c 'del(.[0][-6],.[0][6])'
[[0,1,2,3,5,7,8,9]]
$ echo '[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]' | jq -c 'del(.[-1][-6],.[-1][6])'
[[0,1,2,3,5,7,8,9]]

But if we mix positive and negative indices on the non-leaf level, we get unexpected results:

# appears as if the `6` was deleted first, then `-6` was indexed from the shortened array to delete `3`
$ echo '[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]' | jq -c 'del(.[0][-6],.[-1][6])'
[[0,1,2,4,5,7,8,9]]
# appears as if the `4` was deleted first, then `+6` was indexed from the shortened array to delete `7`
$ echo '[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]' | jq -c 'del(.[-1][-6],.[0][6])'
[[0,1,2,3,5,6,8,9]]

This is consistent with evaluating all negative non-leaf deletions first, then evaluating all positive non-leaf deletions, but not the expected behavior.

The same behavior is evident in delpaths, eg:

$ echo '[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]' | jq -c 'delpaths([[-1,-6],[0,6]])'
[[0,1,2,3,5,6,8,9]]

and in the latest docker version of jq:

$ echo '[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]' | docker run --rm -i ghcr.io/jqlang/jq:latest -c 'delpaths([[-1,-6],[0,6]])'
[[0,1,2,3,5,6,8,9]]

Expected behavior I'd expect behavior consistent with the leaf expectation and all-positive and all-negative cases, that is, for the result to be [[0,1,2,3,5,7,8,9]] every time.

Environment (please complete the following information):

  • OS and Version: Linux (Ubuntu 24.04.4 LTS)
  • jq version: 1.7, but I've verified the same behavior exists on docker run --rm -i ghcr.io/jqlang/jq:latest

Additional context It appears an attempt to group identical indices is made in delpaths_sorted, but this grouping does not normalize negative indices.