Fix: “ERR value is out of range, must be positive” when using SPOP in Redis

If you’re getting an error that reads “ERR value is out of range, must be positive” in Redis, it’s probably because you’re passing a negative count value to the SPOP command.

To fix this issue, make sure the second argument (if supplied) is a positive value.

Example of Error

Here’s an example of code that produces the above error:

SPOP animals -2

Result:

(error) ERR value is out of range, must be positive

In this case I specified a negative value for the second argument, which resulted in the error.

Solution

To fix the issue, simply make sure the second argument (if supplied) is a positive value:

SPOP animals 2

Result:

1) "Horse"
2) "Zebra"

We can also eliminate the error by omitting the second argument altogether:

SPOP animals

Result:

"Bird"

In this case, only one member is popped.

Passing zero will also eliminate the error, but it will also result in an empty array (i.e. no members will be popped):

SPOP animals 0

Result:

(empty array)

Either way, the solution to ensure that we’re not passing a negative value as the second argument.