How to fix ERR WRONGTYPE Operation against a key holding the wrong kind of value in Redis

RedisBEGINNERMEDIUMUpdated Dec 25, 2025· 266 views

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
  1. Check the current data type of the key
  2. Verify you are using the correct command for that data type
  3. Option A: Delete and recreate the key with the correct type
  4. Option B: Rename the key to preserve old data
redis — error
TYPE mykey
ERR 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

  1. Using the wrong Redis command type for a key (e.g., LPUSH on a string key, HGETALL on a list, SADD on a hash)
  2. Code logic change where a key is repurposed with a different data type without cleanup
  3. Database migration or key reuse where old data type does not match new operation type
  4. Multiple services or code paths writing to the same key with different type assumptions
  5. SET command was used to create a string, later code attempts to treat the key as a different type
  6. 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"

1Check the current data type of the key

Use the TYPE command to inspect what data type is actually stored:

redis
TYPE mykey

This 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.

2Verify you are using the correct command for that data type

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.

3Option A: Delete and recreate the key with the correct type

If you own the data and do not need the old value:

redis
DEL mykey

Then create the key with the correct type:

redis
LPUSH mykey value  # for a list
# or
SADD mykey value   # for a set
# or
HSET mykey field value  # for a hash

This is the simplest solution if the old data is no longer needed.

4Option B: Rename the key to preserve old data

If the old data is important for debugging or migration:

redis
RENAME mykey mykey_old

Now create the key with the correct type using the original name:

redis
LPUSH mykey value

This preserves the old data in mykey_old for inspection or rollback.

5Verify you are connected to the correct Redis instance and database

Sometimes WRONGTYPE appears because you are querying the wrong Redis instance or database index:

redis
SELECT 0  # switch to database 0
TYPE mykey

Ensure 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.

6Fix the application code to prevent recurrence

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