# KEYS

> Return every key in the current database that matches a glob pattern.

URL: https://docs.hotkv.com/commands/keys/

```
KEYS pattern
```

- Group: Keys
- Complexity: O(N) where N is the number of keys in the database
- 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](https://docs.hotkv.com/commands/scan/index.md) 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

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Always | Array of bulk strings (empty when nothing matches) | Array |

The order of the keys is not defined.

## Examples

```
127.0.0.1:6379> MSET user:1 "ada" user:2 "grace" order:1 "book"
OK
127.0.0.1:6379> KEYS user:*
1) "user:2"
2) "user:1"
127.0.0.1:6379> KEYS nothing:*
(empty array)
```

## Compatibility

Same as Redis 7 and Valkey 8.

## See also

[SCAN](https://docs.hotkv.com/commands/scan/index.md), [RANDOMKEY](https://docs.hotkv.com/commands/randomkey/index.md),
[EXISTS](https://docs.hotkv.com/commands/exists/index.md)