How to fix WRONGTYPE Operation against a key holding the wrong kind of value in Redis
This error occurs when you try to run a Redis command on a key that stores a different data type than the command expects. For example, using HGETALL (hash command) on a string key will fail. The fix is to check the key's actual type and use the correct command.
Quick fix
Fixes WRONGTYPE Operation against a key holding the wrong kind of value
redis-cli
> TYPE mykey
string- Check the current data type of the key using TYPE
- Delete the key if you want to change its type
- Update your code to use the correct commands for the data type
- Rename the key if you need both old and new data
redis-cli> TYPE mykeystringWRONGTYPE Operation against a key holding the wrong kind of value
What this error means
Redis stores values as specific data types: strings, lists, sets, sorted sets, hashes, streams, bitmaps, and hyperloglogs. Each command is designed to work with only one or a few of these types. When you attempt to run a command against a key that contains a different type, Redis rejects the operation with a WRONGTYPE error. For instance, LPUSH (list command) cannot work on a string key, SADD (set command) cannot work on a hash key, and so forth. This error usually indicates a mismatch between what your application expects to store at a key and what is actually stored there.
Typical symptoms
- Redis client throws "WRONGTYPE Operation against a key holding the wrong kind of value" error
- Command works fine when run on a freshly created key, but fails after data has been stored there before
- Different parts of your code try to use the same key with conflicting data type operations
- Error occurs sporadically in production if the same key is reused for different purposes across multiple deployments
- Cache operations fail because a stale value from a previous operation type still exists
Common causes
- A key was initially set as a string, but your code later tries to use it as a hash or list without deleting it first
- Multiple parts of your application or different code versions use the same key name for different data types
- Reusing Redis key names for different purposes without clearing old data between migrations
- Incorrect assumptions about what data type is stored at a particular key
- Data persistence across deployments where the code logic for a key changes, but the old value remains in Redis
How to fix "WRONGTYPE Operation against a key holding the wrong kind of value"
Connect to Redis CLI and inspect what data type is actually stored at the problematic key:
redis-cli
> TYPE mykey
stringThis tells you the current data type. Common outputs: string, list, set, zset (sorted set), hash, stream, none (key doesn't exist).
Match this type with the commands you are trying to run. For example, if TYPE returns "string" but you are running HGETALL, that's your mismatch.
If the key contains stale data and you want to use it as a different type, delete it first:
redis-cli
> DEL mykey
(integer) 1After deletion, the key no longer exists and your next operation will create it fresh with the new type. Use DEL with caution in production to avoid data loss.
Ensure your code uses commands that match the actual data type at each key:
// If mykey stores a string
redis.get('mykey'); // Correct
redis.hgetall('mykey'); // Wrong - will cause WRONGTYPE
// If mykey stores a hash
redis.hgetall('mykey'); // Correct
redis.lpush('mykey', 'value'); // Wrong - will cause WRONGTYPE
// If mykey stores a list
redis.lpush('mykey', 'value'); // Correct
redis.sadd('mykey', 'member'); // Wrong - will cause WRONGTYPEBe consistent about which data type each key holds throughout your application.
Instead of deleting the key, you can rename it to preserve the old data and free up the name for reuse:
redis-cli
> RENAME mykey mykey_old
OKThis allows you to migrate from one key to another without immediately losing data. You can later decide what to do with mykey_old (archive or delete).
To avoid future conflicts, adopt a naming convention that makes the data type explicit:
// Instead of: user:1
// Use:
redis.set('user:1:profile', jsonString); // string type
redis.hset('user:1:hash', 'name', 'Alice'); // hash type
redis.lpush('user:1:logs', logEntry); // list typePrefixing or suffixing keys with their type makes it clear what operations are valid.
After deleting or renaming the problematic key, test your command again:
redis-cli
> HSET mykey field1 value1
(integer) 1
> HGETALL mykey
1) "field1"
2) "value1"No WRONGTYPE error should appear if the key type now matches the command.
Advanced notes
Atomic Operations: Use Lua scripts (EVAL) when you need to ensure that multiple commands on the same key stay in sync. Lua scripts execute atomically, preventing race conditions where one command changes the key type between checks.
Multiple Databases: If your Redis instance has multiple databases (DB 0, 1, 2, etc.), verify you are connected to the correct database using SELECT before checking types:
> SELECT 0
> TYPE mykeySwitching databases mid-session is a common source of confusion.
Key Expiration: Remember that TTL and EXPIRE work on any type. A key's type determines which data commands work on it, not whether it can expire.
Cluster Considerations: In Redis Cluster, the key must hash to the same slot if you are using it in a pipeline or multi-command transaction. WRONGTYPE errors in cluster setups can sometimes be masked by slot migration issues, so verify cluster status first.
Frequently asked questions
- What does "WRONGTYPE Operation against a key holding the wrong kind of value" mean?
- Redis stores values as specific data types: strings, lists, sets, sorted sets, hashes, streams, bitmaps, and hyperloglogs. Each command is designed to work with only one or a few of these types. When you attempt to run a command against a key that contains a different type, Redis rejects the operation with a WRONGTYPE error.
- How do I fix "WRONGTYPE Operation against a key holding the wrong kind of value"?
- This error occurs when you try to run a Redis command on a key that stores a different data type than the command expects. For example, using HGETALL (hash command) on a string key will fail. The fix is to check the key's actual type and use the correct command.
- What causes "WRONGTYPE Operation against a key holding the wrong kind of value"?
- A key was initially set as a string, but your code later tries to use it as a hash or list without deleting it first Multiple parts of your application or different code versions use the same key name for different data types Reusing Redis key names for different purposes without clearing old data between migrations
- Is the "WRONGTYPE Operation against a key holding the wrong kind of value" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- ๐Redis Official Documentation
- ๐Redis: Error Handling and Protocol(Official Docs)