Redis is an in-memory data structure store used as a database, cache, and message broker. It’s popular due to its high performance and efficiency.
Sometimes, there may be a need to clear or flush the cache to free up space, optimize performance, or remove obsolete data.
This article provides a complete guide on how to flush and clear Redis cache using the command-line interface (CLI), detailing step-by-step instructions to delete all data instantly.
This article Contents
- What is Redis Cache?
- Why Would You Want to Clear the Redis Cache?
- Different Methods to Flush Redis Cache
- Redis CLI Commands for Flushing Cache
- Examples of Flushing Data in Redis
- Flushing Selectively (By Keys or Pattern)
- How to Monitor Redis Cache Usage
- Best Practices for Redis Cache Management
- Conclusion
- FAQs
What is Redis Cache?
Redis (Remote Dictionary Server) is an open-source, in-memory key-value data store that can be used as a database, cache, and message broker. It stores data in memory, enabling low-latency data access.
Redis supports different data structures like strings, hashes, lists, sets, sorted sets, and more. The cache mechanism in Redis allows frequently accessed data to be stored in memory, reducing read times and improving application performance.
Redis is widely used for:
- Caching results of complex queries.
- Session storage in web applications.
- Real-time analytics.
Why Clear the Redis Cache?
Clearing the Redis cache may be necessary in various scenarios:
- Memory optimization: If Redis is consuming too much memory, flushing old or unused data can help free up space.
- Obsolete data: When the cache stores outdated information, it may need to be removed to maintain data accuracy.
- Maintenance tasks: During upgrades, reconfigurations, or application debugging, clearing cache helps ensure no stale data is present.
- Development purposes: In a testing environment, you might want to clear Redis frequently to start with a clean slate.
Note: Clearing the cache removes all stored data. Make sure to back up necessary data before flushing the cache, especially in production environments.
Read also: Chrome net internals dns
Methods to Flush Redis Cache
There are several ways to clear Redis cache using CLI. You can:
- Flush a single database: Clears all keys from a selected Redis database.
- Flush all databases: Clears data from all Redis databases.
- Delete specific keys: Deletes specific keys or data sets based on patterns.
Here’s a breakdown of each method.
Redis CLI Commands for Flushing Cache
You can use the Redis CLI (redis-cli
) to interact with your Redis server and execute various commands, including those for flushing the cache.
1. Flush a Single Redis Database
The FLUSHDB
command clears all keys in the currently selected Redis database.
redis-cli FLUSHDB
This command only affects the active database you are connected to. Redis databases are numbered from 0 by default. You can also specify which database to flush by switching to that database first:
redis-cli -n <DB-Number> FLUSHDB
2. Flush All Databases
The FLUSHALL
command deletes all keys from all Redis databases. This is useful when you want to reset your entire Redis instance.
redis-cli FLUSHALL
You can also use the -a
flag to authenticate if your Redis server is password-protected:
redis-cli -a <password> FLUSHALL
3. Flush Redis Cache Asynchronously
By default, Redis clears the cache synchronously. For large datasets, this can block Redis for some time. You can flush the data asynchronously by appending the ASYNC
option to the FLUSHDB
or FLUSHALL
commands.
redis-cli FLUSHALL ASYNC
This ensures Redis will not be blocked while performing the flush.
Command | Description |
---|---|
FLUSHDB | Flushes the currently selected database. |
FLUSHALL | Flushes all databases. |
FLUSHALL ASYNC | Flushes all databases asynchronously (non-blocking). |
FLUSHDB ASYNC | Flushes the selected database asynchronously. |
Read also: What is chrome://net-internals/dns? How to clear or flush DNS cache on chrome
Examples of Flushing Data in Redis
Let’s walk through practical examples of how to flush Redis cache using CLI.
Example 1: Flushing a Single Database
To clear only the current database (e.g., database 0):
redis-cli -n 0 FLUSHDB
This clears all the keys in database 0 but leaves data in other databases intact.
Example 2: Flushing All Databases
To clear all data from every Redis database:
redis-cli FLUSHALL
This command deletes all data across all Redis databases on the server.
Example 3: Asynchronous Flush
For larger databases where you want to avoid blocking, use the async version:
redis-cli FLUSHALL ASYNC
This ensures the Redis server remains responsive during the flush operation.
Flushing Selectively (By Keys or Pattern)
Sometimes, you might not want to delete everything. Instead, you can target specific keys or patterns. Redis supports key pattern matching using the DEL
and SCAN
commands.
Deleting Specific Keys
To delete a single key from Redis:
redis-cli DEL <key>
Deleting Multiple Keys by Pattern
You can delete multiple keys matching a specific pattern using the following combination of SCAN
and DEL
commands:
redis-cli --scan --pattern '<pattern>' | xargs redis-cli DEL
For instance, to delete all keys starting with “session”:
redis-cli --scan --pattern 'session*' | xargs redis-cli DEL
Tip: Using
SCAN
instead ofKEYS
is more efficient for large datasets, asKEYS
can block the Redis server.
Command | Description |
---|---|
DEL <key> | Deletes the specified key. |
redis-cli --scan --pattern | Efficiently scans and deletes keys based on pattern matching. |
xargs redis-cli DEL | Removes keys matching a specific pattern. |
How to Monitor Redis Cache Usage
Before and after clearing the cache, it’s essential to monitor the memory usage and performance of Redis. The following commands can help:
Check Memory Usage
To check the current memory usage of Redis:
redis-cli INFO MEMORY
This will return detailed statistics, including the total memory used.
Check the Number of Keys
To see how many keys are stored in each database:
redis-cli INFO keyspace
This provides the keyspace statistics, showing the number of keys in each database.
Best Practices for Redis Cache Management
- Regular Monitoring: Regularly check memory usage and performance stats to ensure Redis operates efficiently.
- Backups: Always back up critical data before performing cache flushes, especially in production environments.
- Use Expiry: Set expiry times for cache keys to ensure stale data is automatically deleted.
- Limit the Number of Databases: Redis supports 16 databases by default, but using fewer can simplify cache management.
- Automate Flushing: Automate cache flushing at scheduled intervals using cron jobs or Redis scripts.
Conclusion
Flushing and clearing Redis cache is a simple but critical operation for maintaining a healthy Redis environment. Whether you’re managing a single database or the entire Redis instance, using the appropriate CLI commands like FLUSHDB
, FLUSHALL
, or DEL
helps ensure efficient memory usage and up-to-date data.
By following best practices, including regular monitoring and setting expiration policies, you can optimize your Redis cache management.
FAQs
1. Can I recover data after flushing Redis?
Once data is flushed from Redis, it cannot be recovered unless you have a backup.
2. Is it safe to flush Redis in a production environment?
Flushing Redis in production should be done cautiously. It’s recommended to back up important data and flush caches during low-traffic periods.
3. What is the difference between FLUSHDB
and FLUSHALL
?
FLUSHDB
only clears data from the selected Redis database, while FLUSHALL
clears data from all databases in Redis.
For more information on Redis, visit the official Redis Documentation.
This guide offers a comprehensive look into Redis cache management. Flushing cache through CLI commands is a useful skill to ensure optimal performance and resource management, making it indispensable for developers and sysadmins working with Redis.