Redis command to get all available keys?

Is there a Redis command for fetching all keys in the database? I have seen some python-redis libraries fetching them. But was wondering if it is possible from redis-client.


Solution 1:

Try to look at KEYS command. KEYS * will list all keys stored in redis.

EDIT: please note the warning at the top of KEYS documentation page:

Time complexity: O(N) with N being the number of keys in the database, under the assumption that the key names in the database and the given pattern have limited length.

UPDATE (V2.8 or greater): SCAN is a superior alternative to KEYS, in the sense that it does not block the server nor does it consume significant resources. Prefer using it.

Solution 2:

Updated for Redis 2.8 and above

As noted in the comments of previous answers to this question, KEYS is a potentially dangerous command since your Redis server will be unavailable to do other operations while it serves it. Another risk with KEYS is that it can consume (dependent on the size of your keyspace) a lot of RAM to prepare the response buffer, thus possibly exhausting your server's memory.

Version 2.8 of Redis had introduced the SCAN family of commands that are much more polite and can be used for the same purpose.

The CLI also provides a nice way to work with it:

$ redis-cli --scan --pattern '*'

Solution 3:

It can happen that using redis-cli, you connect to your remote redis-server, and then the command:

KEYS *

is not showing anything, or better, it shows:
(empty list or set)

If you are absolutely sure that the Redis server you use is the one you have the data, then maybe your redis-cli is not connecting to the Redis correct database instance.

As it is mentioned in the Redis docs, new connections connect as default to the db 0.

In my case KEYS command was not retrieving results because my database was 1. In order to select the db you want, use SELECT.
The db is identified by an integer.

SELECT 1
KEYS *

I post this info because none of the previous answers was solving my issue.

Solution 4:

-->Get all keys from redis-cli

-redis 127.0.0.1:6379> keys *

-->Get list of patterns

-redis 127.0.0.1:6379> keys d??

This will produce keys which start by 'd' with three characters.

-redis 127.0.0.1:6379> keys *t*

This wil get keys with matches 't' character in key

-->Count keys from command line by

-redis-cli keys * |wc -l

-->Or you can use dbsize

-redis-cli dbsize

Solution 5:

Take a look at following Redis Cheat Sheet. To get a subset of redis keys with the redis-cli i use the command

KEYS "prefix:*"