Fix: “ERR syntax error” when using SPOP in Redis

If you’re getting an error that reads “ERR syntax error” in Redis, it could be because you’re calling the SPOP command with too many arguments.

At the time of writing, I find that I only get this argument when I pass too many arguments. If I pass no arguments, I get a different error.

In any case, to fix this issue, be sure to pass the correct number of arguments to the command. As of this writing, the command requires at least one argument, and accepts an optional second argument. Any more arguments results in the above error.

Example of Error

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

SPOP animals 1 2

Result:

ERR syntax error

In this case I got the error because I passed too many arguments. Specifically, I passed three arguments, when at the time of running this command, it accepts a maximum of two.

Solution

To fix the above issue, we can simply remove the third argument.

At the time of writing, the syntax for SPOP goes like this:

SPOP key [count]

So we must pass at least one argument (the key for which to perform the operation against), but no more than two arguments.

Here’s an example:

SPOP animals 2

Result:

1) "Dog"
2) "Cat"

This time the command worked fine without error. That’s because I passed only two arguments instead of three – the key, and the optional member count.

See the Redis documentation to check the latest specification for this command (in case anything changes after this article is published).