Dragonfly

Node Redis Delete All Keys (Detailed Guide w/ Code Examples)

Use Case(s)

In Node.js applications using Redis as a cache or session store, you may need to delete all keys in scenarios such as:

Code Examples

  1. Using flushdb command:

CODE_BLOCK_PLACEHOLDER_0
flushdb is a Redis command that deletes all keys from the current database. This method is handy when you want to clear your entire cache.

  1. Using keys and del commands:

CODE_BLOCK_PLACEHOLDER_1
In this example, we first call the keys command with the wildcard '*' to fetch all keys in the database. After that, we loop over the keys and delete each one using the del command.

Best Practices

Common Mistakes

FAQs

Q: Can I delete keys matching a specific pattern?
A: Yes, in the second code example, replace '' in the keys method with your pattern. For example, 'session:' will match all keys that start with 'session:'.

Was this content helpful?

Similar Code Examples

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

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