How to fix ERR WRONGTYPE Operation against a key holding the wrong kind of value in Redis
This error occurs when a Redis command is executed on a key that holds a different data type than expected. Redis enforces strict type checking—using LPUSH on a string key or HGETALL on a list will fail. Fix by checking the key type with TYPE, using the correct command for that type, or deleting and recreating the key with the proper data type.
Quick fix
Fixes WRONGTYPE
TYPE mykey- Check the current data type of the key
- Verify you are using the correct command for that data type
- Option A: Delete and recreate the key with the correct type
- Option B: Rename the key to preserve old data
TYPE mykeyERR WRONGTYPE Operation against a key holding the wrong kind of value
What this error means
Redis is a data structure store with multiple data types: strings, lists, sets, hashes, and sorted sets. Each key holds exactly one type of value at a time. When you execute a command designed for one data type on a key containing a different type, Redis returns the WRONGTYPE error to prevent data corruption and semantically invalid operations. For example, you cannot push an item to a hash, or retrieve members from a string value using set commands. This strict type checking ensures data integrity and prevents misuse of Redis data structures.
Typical symptoms
- Commands fail with "ERR WRONGTYPE" error messages in application logs
- Application crashes or runtime exceptions when accessing Redis keys
- Inconsistent behavior where some operations work but others fail on the same key
- Commands that worked previously suddenly start failing
- Data operations silently fail in client libraries without errors
Common causes
- Using the wrong Redis command type for a key (e.g., LPUSH on a string key, HGETALL on a list, SADD on a hash)
- Code logic change where a key is repurposed with a different data type without cleanup
- Database migration or key reuse where old data type does not match new operation type
- Multiple services or code paths writing to the same key with different type assumptions
- SET command was used to create a string, later code attempts to treat the key as a different type
- Shared key namespace where different application components expect incompatible data types
How to fix "ERR WRONGTYPE Operation against a key holding the wrong kind of value"
Use the TYPE command to inspect what data type is actually stored:
TYPE mykeyThis returns the data type (string, list, set, hash, zset, stream, none, etc.). Run this in the Redis CLI or through your client library. This tells you what the key actually contains.
Match your command to the actual data type:
For strings: GET, SET, INCR, DECR, APPEND
For lists: LPUSH, RPUSH, LPOP, RPOP, LRANGE, LLEN
For sets: SADD, SREM, SMEMBERS, SISMEMBER, SUNION
For hashes: HSET, HGET, HGETALL, HDEL, HEXISTS
For sorted sets: ZADD, ZRANGE, ZREM, ZSCORE, ZCARD
If your code uses the wrong command category, update it to match the key's actual type.
If you own the data and do not need the old value:
DEL mykeyThen create the key with the correct type:
LPUSH mykey value # for a list
# or
SADD mykey value # for a set
# or
HSET mykey field value # for a hashThis is the simplest solution if the old data is no longer needed.
If the old data is important for debugging or migration:
RENAME mykey mykey_oldNow create the key with the correct type using the original name:
LPUSH mykey valueThis preserves the old data in mykey_old for inspection or rollback.
Sometimes WRONGTYPE appears because you are querying the wrong Redis instance or database index:
SELECT 0 # switch to database 0
TYPE mykeyEnsure your client library is connecting to the right Redis instance (correct host, port, and database index). Verify the key exists in the database you expect.
Review the code that triggered the error:
1. Verify all keys are used consistently throughout your codebase with the same data type
2. Add comments documenting the expected type for each key (e.g., // users:1 -> hash)
3. Use separate key prefixes or namespaces for different data types to avoid reuse (e.g., user:1:name for strings, user:1:tags for sets)
4. In client libraries, use type-safe abstractions or helper functions that enforce the correct operation for a key
5. Add unit tests that validate the data type before performing operations
This ensures future developers do not accidentally mix types on the same key.
Advanced notes
Atomic Rename with Transactions: When renaming a key to preserve old data, use RENAME with WATCH/MULTI/EXEC for atomicity in high-concurrency scenarios to prevent race conditions.
SET Command Special Behavior: The SET command is unique—it overwrites any existing key regardless of type. Other commands like LPUSH, SADD, ZADD, HSET will reject a key of the wrong type without modifying it. SET is type-agnostic by design.
Data Type Evolution: Be careful with key reuse patterns. If a key starts as a string but you later want to use it as a list, explicitly delete or rename it first. Redis clients cannot infer your intent.
Expiration and Type: Using EXPIRE on a key does not change its type. A WRONGTYPE error persists until the key expires naturally or is explicitly deleted.
Frequently asked questions
- What does "WRONGTYPE" mean?
- Redis is a data structure store with multiple data types: strings, lists, sets, hashes, and sorted sets. Each key holds exactly one type of value at a time. When you execute a command designed for one data type on a key containing a different type, Redis returns the WRONGTYPE error to prevent data corruption and semantically invalid operations.
- How do I fix "WRONGTYPE"?
- This error occurs when a Redis command is executed on a key that holds a different data type than expected. Redis enforces strict type checking—using LPUSH on a string key or HGETALL on a list will fail. Fix by checking the key type with TYPE, using the correct command for that type, or deleting and recreating the key with the proper data type.
- What causes "WRONGTYPE"?
- Using the wrong Redis command type for a key (e.g., LPUSH on a string key, HGETALL on a list, SADD on a hash) Code logic change where a key is repurposed with a different data type without cleanup Database migration or key reuse where old data type does not match new operation type
- Is the "WRONGTYPE" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚Redis Official Documentation
- 📖Redis Commands Reference(Official Docs)