Complete Guide to Flushing and Clearing Redis Cache Using CLI: How to Delete All Data Instantly

Er Hana By Er Hana
9 Min Read

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

  1. What is Redis Cache?
  2. Why Would You Want to Clear the Redis Cache?
  3. Different Methods to Flush Redis Cache
  4. Redis CLI Commands for Flushing Cache
  5. Examples of Flushing Data in Redis
  6. Flushing Selectively (By Keys or Pattern)
  7. How to Monitor Redis Cache Usage
  8. Best Practices for Redis Cache Management
  9. Conclusion
  10. 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:

  1. Flush a single database: Clears all keys from a selected Redis database.
  2. Flush all databases: Clears data from all Redis databases.
  3. 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.


CommandDescription
FLUSHDBFlushes the currently selected database.
FLUSHALLFlushes all databases.
FLUSHALL ASYNCFlushes all databases asynchronously (non-blocking).
FLUSHDB ASYNCFlushes 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 of KEYS is more efficient for large datasets, as KEYS can block the Redis server.

CommandDescription
DEL <key>Deletes the specified key.
redis-cli --scan --patternEfficiently scans and deletes keys based on pattern matching.
xargs redis-cli DELRemoves 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

  1. Regular Monitoring: Regularly check memory usage and performance stats to ensure Redis operates efficiently.
  2. Backups: Always back up critical data before performing cache flushes, especially in production environments.
  3. Use Expiry: Set expiry times for cache keys to ensure stale data is automatically deleted.
  4. Limit the Number of Databases: Redis supports 16 databases by default, but using fewer can simplify cache management.
  5. 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.


You’ll also like to read:

Share This Article
By Er Hana
Here, I'm Masroor, the founder of Grey Knight, is a seasoned programmer, web developer, and technology enthusiast with a degree in Computer Science from Punjab Technical University (PTU). With a passion for demystifying complex tech concepts, I Have dedicated my career to providing reliable and easy-to-follow solutions for troubleshooting web, internet, and technology issues.
Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *