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.
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.
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.
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.
ERR Unbalanced XREAD list of streams
How to fix "ERR Unbalanced XREAD list" in Redis
ERR syntax error
How to fix "ERR syntax error" in Redis
ConnectionError: Error while reading from socket
ConnectionError: Error while reading from socket in redis-py
ERR unknown command
How to fix ERR unknown command in Redis
Command timed out
How to fix 'Command timed out' in ioredis