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

RedisINTERMEDIATEHIGHUpdated Dec 29, 2025ยท 226 views

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
  1. Check the current data type of the key using TYPE
  2. Delete the key if you want to change its type
  3. Update your code to use the correct commands for the data type
  4. Rename the key if you need both old and new data
redis โ€” error
redis-cli
> TYPE mykey
string
WRONGTYPE 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

  1. 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
  2. Multiple parts of your application or different code versions use the same key name for different data types
  3. Reusing Redis key names for different purposes without clearing old data between migrations
  4. Incorrect assumptions about what data type is stored at a particular key
  5. 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"

1Check the current data type of the key using TYPE

Connect to Redis CLI and inspect what data type is actually stored at the problematic key:

bash
redis-cli
> TYPE mykey
string

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

2Delete the key if you want to change its type

If the key contains stale data and you want to use it as a different type, delete it first:

bash
redis-cli
> DEL mykey
(integer) 1

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

3Update your code to use the correct commands for the data type

Ensure your code uses commands that match the actual data type at each key:

javascript
// 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 WRONGTYPE

Be consistent about which data type each key holds throughout your application.

4Rename the key if you need both old and new data

Instead of deleting the key, you can rename it to preserve the old data and free up the name for reuse:

bash
redis-cli
> RENAME mykey mykey_old
OK

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

5Use different key names for different data types

To avoid future conflicts, adopt a naming convention that makes the data type explicit:

javascript
// 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 type

Prefixing or suffixing keys with their type makes it clear what operations are valid.

6Verify the fix by re-running the command

After deleting or renaming the problematic key, test your command again:

bash
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:

bash
> SELECT 0
> TYPE mykey

Switching 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