GrepBoard/Manuals
605 manuals ·

Redis WRONGTYPE Operation against a key holding the wrong kind

·Tested on Redis 7.4.12 (Docker redis:7) · 2026-10·Test scope ↓·6 steps

Redis WRONGTYPE Operation against a key means the command does not match the key's data type. Check the real type with TYPE, then fix the command or key name.

output
WRONGTYPE Operation against a key holding the wrong kind of value

The key exists, but its data type does not match the command: HGET on a string key, GET or LPUSH on a hash key. The data is not corrupted. Usually two parts of an app use the same key name with different types, or the code expects a type that has since changed. TYPE shows the real type and tells you which side is wrong.

console
$ redis-cli HGET session:42 userWRONGTYPE Operation against a key holding the wrong kind of value $ redis-cli GET user:1WRONGTYPE Operation against a key holding the wrong kind of value 

[1] Check the type of the failing key. If the app log does not show the key name, find where the failing code builds it.

console
$ for k in session:42 user:1 queue:mail tags:post:9 rank:daily nosuch; do printf "%s %s\n" "$k" "$(redis-cli TYPE "$k")"; donesession:42 stringuser:1 hashqueue:mail listtags:post:9 setrank:daily zsetnosuch none

none means the key does not exist, and a missing key never causes this error (reads return empty, writes create the key).

[2] Read it with a command for its type. For manual checks:

TYPEReadWrite
stringGETSET · INCR
hashHGET · HGETALLHSET
listLRANGE key 0 -1LPUSH · RPUSH
setSMEMBERSSADD
zsetZRANGE key 0 -1 WITHSCORESZADD
console
$ redis-cli HGETALL user:1namealiceplanpro

OBJECT ENCODING shows the internal storage (embstr, listpack, …), not the type; use TYPE to tell types apart.

[3] Find keys under the same naming scheme with a different type. When a release moves a cache from string (JSON) to hash, old and new keys coexist for a while and only some requests fail. Split them by type with the TYPE option of SCAN:

console
$ redis-cli SCAN 0 MATCH "cache:user:*" TYPE hash COUNT 10000cache:user:2$ redis-cli SCAN 0 MATCH "cache:user:*" TYPE string COUNT 10000cache:user:1cache:user:3

The first line is the next cursor; 0 means the scan is complete. On a large keyspace, repeat until the cursor returns to 0. Use SCAN rather than KEYS in production: KEYS walks the whole keyspace in one go and other commands wait meanwhile.

[4] If key names collide, separate them. When one feature stores user:1 as a hash and another as a string, give one of them a prefix such as user:1:profile or cache:user:1, or the error will come back.

[5] To clear old-format keys, delete or rename them; do not overwrite them with SET. SET overwrites the key whatever its type and drops any TTL (SET). In the test, the hash user:1 became a string after one SET and lost its fields, and a key with a 600-second TTL showed -1 (no expiry) after SET.

console
$ redis-cli SET user:1 overwrittenOK$ redis-cli TYPE user:1string$ redis-cli SET cache:user:1 "{\"name\":\"alice2\"}"; redis-cli TTL cache:user:1OK-1

For cache keys that can be rebuilt, delete them and let the app write the new format. If deleting right away feels risky, rename first and delete after checking. UNLINK frees a large key's memory in the background.

console
$ redis-cli RENAME cache:user:2 cache:user:2:oldOK$ redis-cli UNLINK cache:user:2:old1

RENAME silently deletes and overwrites the target if a key with the new name already exists. If that can happen, use RENAMENX, which refuses when the new name exists (a result of 0 means nothing was renamed). If the app misbehaves after the rename, put it back with RENAME cache:user:2:old cache:user:2 before you UNLINK.

For primary data (sessions, carts, queues), dump the value somewhere before you delete — a deleted key cannot be brought back.

[6] Inside MULTI, the other commands still run when this error hits. Per the docs, an error after EXEC fails only that command, and there is no rollback (Transactions).

console
$ printf "MULTI\nSET a abc\nLPOP a\nINCR hits\nEXEC\n" | redis-cliOKQUEUEDQUEUEDQUEUEDOKWRONGTYPE Operation against a key holding the wrong kind of value 1

SET a and INCR hits were applied. Code that handles the transaction result must check each element of the reply array, or it misses a partial apply.

#redis#wrongtype#type#data-types#scan