KEYS
KEYS pattern
- Group
- Keys
- Complexity
- O(N) where N is the number of keys in the database
- Arity
- 2 (exactly, counting the command name)
- Flags
- read-only, slow
- HotKV Cloud
- All plans
- On-Premise
- no license needed
KEYS returns all key names in the selected database that match pattern. It
looks at every key in one call, so on a large database it can take a long
time. Prefer SCAN in production code and keep KEYS for
debugging and small datasets. In a cluster it returns the keys of the node you
ask only.
Patterns use glob syntax:
| Pattern | Matches |
|---|---|
* |
Any sequence of characters, including none |
? |
Exactly one character |
[abc] |
One of the listed characters |
[^a] |
Any character except the listed ones |
[a-z] |
One character in the range |
\ |
Escapes the next character, for example \* for a literal * |
Return value
Section titled “Return value”| Case | RESP2 | RESP3 |
|---|---|---|
| Always | Array of bulk strings (empty when nothing matches) | Array |
The order of the keys is not defined.
Examples
Section titled “Examples”127.0.0.1:6379> MSET user:1 "ada" user:2 "grace" order:1 "book"OK127.0.0.1:6379> KEYS user:*1) "user:2"2) "user:1"127.0.0.1:6379> KEYS nothing:*(empty array)Compatibility
Section titled “Compatibility”Same as Redis 7 and Valkey 8.
See also
Section titled “See also”Was this page helpful?
Thanks for the feedback.