w3resource

Redis KEYS

Redis KEYS Command

Redis KEYS command is used to return all keys matching a pattern.

Supported glob-style patterns:

  • h?llo matches hello, hallo and hxllo
  • h*llo matches hllo and heeeello
  • h[ae]llo matches hello and hallo, but not hillo
  • h[^e]llo matches hallo, hbllo, ... but not hello
  • h[a-b]llo matches hallo and hbllo

Use \ to escape special characters if you want to match them verbatim.

Here are some common uses and scenarios for the "KEYS" command:

  • Debugging and Development: Quickly find and inspect keys during development.
  • Data Management: Identify and manage groups of related keys.
  • Cleanup: Locate and delete unused or expired keys.
  • Backup and Migration: List keys for export or migration purposes.

Syntax:

KEYS PATTERN

Available since

1.0.0.

Return Value

Array Reply : List of keys matching pattern (Array).

Return Value Type

Array

Example: Redis KEYS

First, create a key in redis and set some value in it.

127.0.0.1:6379> MSET key 1 key1 2 key2 3 key3 4
OK
127.0.0.1:6379> KEYS *e*
 1) "key"
 2) "user-p"
 3) "keys"
 4) "user"
 5) "languages"
 6) "user-y"
 7) "numbers"
 8) "user-v"
 9) "user-x"
10) "hash key"
11) "key3"
12) "key1"
13) "key2"
127.0.0.1:6379> KEYS s??
(empty list or set)
127.0.0.1:6379> KEYS *
 1) "key"
 2) "user-p"
 3) "langhash"
 4) "keys"
 5) "user"
 6) "xhash"
 7) "languages"
 8) "user-y"
 9) "lang"
10) "numbers"
11) "user-v"
12) "user-x"
13) "hash key"
14) "myhash"
15) "key3"
16) "key1"
17) "key2"

Previous: EXPIREAT
Next: MOVE



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/redis/redis-key-pattern.php