Question: How can you clear a distributed cache?
Answer
Clearing a distributed cache typically involves deleting its entries, either one at a time or all at once. The method to do so however depends on the specific caching system you are using. Here are examples for three popular systems: Redis, Memcached, and Apache Ignite.
Redis
To delete all keys from all databases in Redis, you use the FLUSHALL command.
redis-cli FLUSHALLIf you want to delete all keys from the currently selected database (default is database 0), you use the FLUSHDB command.
redis-cli FLUSHDBMemcached
For memcached, you can invalidate all existing cache items using the flush_all command. Below is an example of how to flush everything from a server running on localhost on port 11211.
echo 'flush_all' | nc localhost 11211This command does not remove items immediately, but rather invalidates them - they will be removed as and when Memcached needs to reclaim memory.
Apache Ignite
With Apache Ignite, you can clear caches with the removeAll() method, which removes all entries from cache.
IgniteCache cache = ignite.cache("myCache");
cache.removeAll();Remember that clearing cache should be done carefully, considering its impact on your application's performance and functionality.
Was this content helpful?
Help us improve by giving us your feedback.
Other Common In Memory Questions (and Answers)
- What is a persistent object cache and how can one implement it?
- How can I set up and use Redis as a distributed cache?
- What are the differences between an in-memory cache and a distributed cache?
- What is the best distributed cache system?
- Is Redis a distributed cache?
- How can you implement a distributed cache using Docker?
- What are the differences between a centralized cache and a distributed cache?
- What is the best distributed cache for Java?
- What is the difference between distributed cache and local cache?
- How can we implement a distributed LRU (Least Recently Used) cache?
- How can I implement in-memory caching in AWS Lambda?
- What are the differences between Memcached and in-memory caching?
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost