How to Use HGETALL Command in Redis: Syntax and Examples
Use the
HGETALL command in Redis to get all fields and their values from a hash stored at a key. It returns a list of field-value pairs, allowing you to see the entire hash content in one call.Syntax
The HGETALL command retrieves all fields and values of a hash stored at a given key.
Syntax parts:
HGETALL key: key is the name of the hash you want to read.- The command returns a list of fields and values in alternating order.
redis
HGETALL key
Example
This example shows how to create a hash with user information and then retrieve all its fields and values using HGETALL.
redis
127.0.0.1:6379> HSET user:1000 name "Alice" age "30" city "Paris" (integer) 3 127.0.0.1:6379> HGETALL user:1000 1) "name" 2) "Alice" 3) "age" 4) "30" 5) "city" 6) "Paris"
Output
1) "name"
2) "Alice"
3) "age"
4) "30"
5) "city"
6) "Paris"
Common Pitfalls
Common mistakes when using HGETALL include:
- Using
HGETALLon a key that does not exist returns an empty list. - Expecting a dictionary or map directly; Redis returns a flat list alternating fields and values.
- Confusing
HGETALLwith commands that return only keys or values separately.
Always check if the key exists before using HGETALL to avoid empty results.
redis
127.0.0.1:6379> HGETALL missingkey (empty list or set) # Correct way to handle: 127.0.0.1:6379> EXISTS missingkey (integer) 0
Output
(empty list or set)
(integer) 0
Quick Reference
| Command | Description | Return Type |
|---|---|---|
| HGETALL key | Get all fields and values in the hash stored at key | List of field-value pairs |
| HGET key field | Get the value of a specific field in the hash | String or nil |
| HEXISTS key field | Check if a field exists in the hash | Integer (0 or 1) |
| HSET key field value | Set the value of a field in the hash | Integer (1 if new field, 0 if updated) |
Key Takeaways
Use HGETALL to retrieve all fields and values from a Redis hash in one command.
HGETALL returns a flat list alternating field names and their values, not a dictionary.
If the key does not exist, HGETALL returns an empty list.
Check key existence with EXISTS before using HGETALL to handle missing hashes gracefully.
HGETALL is useful for quickly inspecting the full content of a hash.