πŸ”§ Error Fixes
Β· 1 min read

Redis: WRONGTYPE Operation β€” How to Fix It


WRONGTYPE Operation against a key holding the wrong kind of value means you’re using a string command on a list, or a list command on a hash, etc.

Why this happens

Redis keys are typed β€” each key holds exactly one data structure (string, list, hash, set, or sorted set). Commands are type-specific, so running GET on a list or LPUSH on a string fails immediately. This commonly occurs when you change your caching strategy but old keys with the previous type still exist in Redis.

Fix 1: Check the key type

redis-cli TYPE mykey
# Returns: string, list, hash, set, zset, or none

Fix 2: Use the right command

# String: GET, SET, INCR
# List: LPUSH, RPUSH, LRANGE, LPOP
# Hash: HGET, HSET, HGETALL
# Set: SADD, SMEMBERS, SISMEMBER
# Sorted Set: ZADD, ZRANGE, ZSCORE

Fix 3: Delete and recreate with correct type

DEL mykey
# Now set it with the correct type
SET mykey "value"        # string
# or
LPUSH mykey "value"      # list

Alternative solution: Use namespaced keys

Prefix keys with their intended type or purpose to avoid collisions:

SET user:123:name "Alice"       # string
LPUSH user:123:history "login"  # list
HSET user:123:profile name "Alice" age 30  # hash

Prevention

  • Use key naming conventions (e.g., cache:users:list, session:abc:hash) so the expected type is clear from the key name.
  • When changing data structures in your code, run a migration script to delete or convert old keys first.

Related: Redis cheat sheet Β· Redis OOM / maxmemory fix Β· Redis Connection Refused