Fix: “ERR wrong number of arguments for ‘spop’ command” in Redis

If you’re getting an error that reads “ERR wrong number of arguments for ‘spop’ command” in Redis, it’s because you’re calling the SPOP command with the wrong number of arguments.

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

In any case, to fix this issue, be sure to pass at least one argument to the command (and at the time of writing, no more than two).

Example of Error

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

SPOP

Result:

(error) ERR wrong number of arguments for 'spop' command

In this case I didn’t pass any arguments, and so I got the error.

Solution

To fix the issue, simply pass the correct number of arguments. More specifically, at least pass the name of the key that you want to perform the operation against.

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.

Here’s an example:

SPOP animals

Result:

"Zebra"

In this case, I only passed one argument (the key), and so only one member was popped. We can alternatively provide a second argument to determine how many keys to pop.

Example:

SPOP animals 2

Result:

1) "Bird"
2) "Cow"

Either way, the solution to the error is to pass at least one argument, being the name of the key to perform the operation against.

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