TL;DR
Here is how to get all keys in Redis using CLI:
- Connect to Redis using
redis-cli, then use theKEYS *command to retrieve all keys or specific patterns like user* for quick debugging or development checks. - Use the
SCANcommand for a non-blocking, cursor-based iteration over the key space, suitable for large datasets, it requires multiple calls to retrieve all keys and allows adjusting retrieval volume with theCOUNToption for efficiency.
Read the guide below to find out how to get all keys in Redis CLI on Ubuntu and the common problems that can happen.
If you’re trying to manage Redis keys using the command line, it can feel a bit tricky, especially with a large database or unexpected issues. In this guide, I’ll show you how to effectively use the KEYS and SCAN commands so you can easily get all keys in Redis or find specific ones. Whether you’re working in a small test environment or managing keys in a busy production setting, this guide will help streamline your process. I’ll walk through each command step-by-step, covering everything from initiating a connection to handling the output.
What are Keys in Redis?
Keys in Redis are unique identifiers that help you store, retrieve, and manage various types of data, such as strings, lists, sets, and hashes. Each key is paired with a value and is distinct within a Redis database. This unique setup allows for quick data operations and retrieval.
Redis keys can also have an expiry time, known as Time to Live (TTL). This feature lets you set a duration after which the key automatically deletes itself, making it useful for temporary data storage like session information or cache management. This ensures that the database uses memory efficiently by removing outdated or unnecessary data automatically.
How to Get All Keys in Redis?
To get all keys in Redis, first connect to the Redis server by typing redis-cli. Once connected, use the KEYS * command to display all keys stored in Redis. This command will return a list of all keys in the current database. Note that using KEYS * can have a significant performance impact on large databases, so in production environments, it’s recommended to use SCAN instead for iterative scanning.
That was the quick answer. Here is the detailed step-by-step guide for each method to get all keys in Redis CLI:
1. KEYS Command
The KEYS command in Redis is used to retrieve all the keys matching a specified pattern. This command is straightforward and useful for debugging or when you need a quick glance at what keys are in your database during the development phases. Follow these steps:
- Open your Terminal on Ubuntu and connect to Redis by typing the command:
redis-cli It will connect you to your Redis server via the command line interface.

- To list all keys in your Redis database, execute the command:
KEYS * This command will return a list of all keys.

- If you are looking for keys that match a specific pattern, replace * with your pattern, such as
user*for all keys that start with user.

2. SCAN Command
The SCAN command is a cursor-based iterator that allows you to iterate over the key space incrementally. This command is designed to provide a non-blocking way to scan through keys, making it suitable for use even in production environments with large datasets. Here is the step-by-step guide:
- Start by accessing your Redis CLI on your Ubuntu system.
- Initiates the scan operation from the beginning of the database.