Fix “WRONGTYPE Operation against a key holding the wrong kind of value” when using ZREM in Redis

If you get an error that reads “WRONGTYPE Operation against a key holding the wrong kind of value” when using the ZREM command in Redis, it’s because you’re passing a key with the wrong data type.

To fix this issue, make sure that you pass a sorted set to the ZREM command.

Example of Error

Here’s an example of code that causes the error:

ZREM animals "Meow" "Scratch"

Result:

(error) WRONGTYPE Operation against a key holding the wrong kind of value

In my case, the animals key holds a set (not a sorted set), which is why I got the error.

We can use the TYPE command to check a key’s type:

TYPE animals

Result:

set

As suspected, the key holds a set, which is the wrong data type for the ZREM command.

Solution

The solution is to make sure the key we pass to the ZREM command holds a sorted set.

Let’s replace the animals key with another key that holds a sorted set:

ZREM cats "Meow" "Scratch"

Result:

(integer) 2

This time the two members were removed as specified without error. That’s because the cats key holds a sorted set.

Let’s check:

TYPE cats

Result:

zset

As expected, it’s a sorted set.