Redis

WRONGTYPE Operation against a key holding the wrong kind of value

WRONGTYPE Operation against a key holding the wrong kind of value: what causes it, how to check a key's real type, and how to fix the command.

What it means

Redis returns WRONGTYPE Operation against a key holding the wrong kind of value when a command is run against a key whose stored data type doesn’t match what that command expects — for example, calling LPUSH on a key that already holds a string, or HGET on a key that’s actually a set. Every key in Redis has exactly one type at a time, and Redis enforces that strictly rather than silently coercing the data. (SET is the one common exception: it overwrites a key of any type, discarding the old value.)

Common causes

  • The key already exists with a different type, often because an earlier SET created it as a string before a later HSET or SADD tried to reuse the same name for a hash or set.
  • A typo or copy-pasted key name that happens to collide with an existing key of a different purpose.
  • A key-naming convention without a type suffix, making it easy for two different parts of an application to disagree about what a given key should hold.
  • Reusing a counter or flag key (typically a string, from INCR or SET) for a different structure later in the same codebase.

How to fix it

  1. Check the key’s actual type before assuming what it holds: TYPE mykey. It returns string, list, set, zset, hash, stream (or vectorset on Redis 8), or none if the key doesn’t exist.
  2. If the key was created with the wrong type by mistake, delete it and recreate it with the intended type: DEL mykey then the correct command (HSET, SADD, RPUSH, etc.).
  3. If two different features genuinely need two different structures, give them different key names — a naming convention that encodes the type (user:1:profile as a hash, user:1:sessions as a set) prevents this class of collision.
  4. In application code, check TYPE (or catch the WRONGTYPE error explicitly) before an operation that depends on the key already existing with a specific type, rather than assuming it.
  5. If this shows up after a deploy, check for a recent change to which command writes that key — a refactor that switched from SET to HSET without migrating existing keys is a common trigger.

In Tablyne

Tablyne’s key inspector shows a key’s type in its header as soon as you open it, and the keyspace sidebar can filter keys by type, so you can see that a key is a string rather than a hash before you run a command against it, instead of finding out from a WRONGTYPE error.

See every key’s real type before you touch it. Download Tablyne or open the Redis & Valkey GUI.

Sources

Frequently asked questions

Why does the same key name work in one command and not another? +

Every Redis key has exactly one data type (string, list, set, hash, sorted set, stream, and a few others) for as long as it exists. A command that expects a different type than what's actually stored — HGET on a string, LPUSH on a set — always fails with WRONGTYPE, regardless of the key's name or when it was created.

Does WRONGTYPE mean the key is corrupted? +

No. It's Redis correctly enforcing that a key has one fixed type. It usually means the application (or a script, or a colleague) wrote to that key with a different command than the one now reading it, or the key name collided with an existing key of another type.

How do I find out what type a key already is? +

Run TYPE <key>. It returns one of string, list, set, zset, hash, stream (or vectorset), or none if the key doesn't exist — checking it before an operation avoids the error entirely.

Related errors