Fix “WRONGTYPE Operation against a key holding the wrong kind of value” When Calling SUNION or SUNIONSTORE in Redis

If you’re getting an error that reads “WRONGTYPE Operation against a key holding the wrong kind of value” when calling either the SUNION command or the SUNIONSTORE command in Redis, it’s probably because you’re passing a key with the wrong data type.

To fix this issue, make sure that each key that you pass holds a set.

Example of Error

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

SUNION cats name

Result:

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

Solution

To fix the issue, make sure all the keys contain sets.

In my example, the name key actually holds a string, and that’s why I got the error.

Here’s what happens when I replace that key with a key of the correct type:

SUNION cats dogs

Result:

1) "Bark"
2) "Scratch"
3) "Fluffy"
4) "Meow"
5) "Wag"

I get the desired result without error.