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

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

To fix this issue, make sure you pass the correct number of arguments. At the time of writing, the SREM command requires at least two arguments.

Example of Error

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

SREM animals

Result:

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

The same thing happens when we call the command without any arguments:

SREM

Result:

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

Solution

To fix the issue, simply pass at least two arguments when running the command.

The syntax for SREM goes like this:

SREM key member [member ...]

So, we must pass at least two arguments – one for the key, and one or more for the member/s to remove.

Example:

SREM animals Cat

Result:

(integer) 1

Here, I provided two arguments. The first one specifies the key, and the second is the member to remove from that key. One member was removed, as indicated in the result.

As mentioned, we can specify more than one member to remove. Here’s an example:

SREM animals Dog Horse Buffalo

Result:

(integer) 2

In this case, only two of the three members were removed. That’s because one of the members didn’t exist at that key.

We can also eliminate the error by passing members that don’t exist in the key:

SREM animals Dog Horse Buffalo

Result:

(integer) 0

We had already removed those members in the previous example, so they no longer exist in that key.

We can also eliminate the error by passing a key that doesn’t exist:

SREM nonexistentkey Dog Horse Buffalo

Result:

(integer) 0

The above information is true at the time of writing. See the Redis documentation to check the latest specification for this command.