4 Effective Ways to Delete Multiple Keys in Redis CLI

Written by

Reviewed by

Last updated: May 10, 2024

Expert verified

SVG Image

TL;DR

Here is how to delete multiple keys in Redis:

  1. DEL Command: Use DEL key1 key2 key3 in Redis to remove specific keys like key1, key2, and key3 in one operation.
  2. FLUSHDB Command: Use FLUSHDB to delete all keys in the currently selected Redis database instantly, without affecting other databases.

Cleaning up or resetting your Redis database is a routine yet crucial task, especially during development, testing, or regular maintenance. In this guide, I’ll walk you through the different methods to delete multiple keys in Redis efficiently and safely. I’ll cover everything from using the DEL command for targeting specific keys to employing FLUSHDB and FLUSHALL for broader sweeps. You’ll also learn how to use wildcard patterns for bulk deletions. By following these detailed steps and adhering to best practices, you’ll be well-equipped to manage key deletions in various scenarios and safeguard your data effectively.

How to Delete Multiple Keys in Redis?

To delete multiple keys in Redis, you can use the DEL command followed by the keys you wish to remove. For example, if you want to delete the keys key1, key2, and key3, you would issue the command DEL key1 key2 key3. This command will remove the specified keys from the database if they exist, and it will return the number of keys that were actually deleted. This method is direct and efficient for removing multiple keys simultaneously.

That was the quick answer. To learn to delete multiple keys in Redis using four different methods, continue reading the article below:

1. DEL Command

The DEL command in Redis is used to delete one or more keys. It’s a direct and efficient way to remove individual keys or small sets of keys, ensuring they are no longer available in the dataset. Follow these steps to delete keys using the del command:

  1. Open your Terminal window. 
open terminal
  1. Connect to your Redis server by typing: 
redis-cli
connecting to redis cli
  1. To delete a single, run the following command:
DEL guestuserkey

This command will remove the key named guestuserkey from Redis. Replace the guestuser key with key that you actually want to remove.

deleting a key in redis using DEL
  1. To delete multiple keys in Redis, enter the following command:
DEL key1 key2 key3

This command will remove key1, key2, and key3 from Redis, all in a single operation.

deleting multiple keys at a time

2. FLUSHDB Command

The FLUSHDB command is used to delete all keys in Redis of the currently selected database without affecting other databases. This method is suitable when you need to completely reset a database but do not wish to impact the entire instance. Here is how to do it:

  1. Access your Redis session through your Terminal.
  2. Run the following command to delete all keys in Redis in a specific database:
FLUSHDB

Executing this command will remove all keys from the currently selected Redis database instantly.

deleting keys from a specific database

3. FLUSHALL Command

FLUSHALL deletes all the keys from all databases in the Redis server, making it the most extensive cleaning operation. It is highly impactful and should be used with caution. Here is the step-by-step guide to delete all keys in Redis using FLUSHALL:

  1. Enter the redis-cli in your command line, and to clear all databases, execute the following command:
FLUSHALL

This command clears all data from every database in the Redis server, effectively resetting the server to a clean state.

deleting keys from all databases

4. Wildcards for Bulk Deletion

Redis supports pattern matching which can be utilized to delete multiple keys in Redis based on naming conventions. This is particularly useful for managing keys in larger datasets or when keys are dynamically named. Here is how to do it

  1. In Redis CLI, run the following command:
EVAL "return redis.call('del', unpack(redis.call('keys', 'pattern*')))" 0

Replace pattern* with the pattern that matches the keys you want to delete

This script uses Lua scripting within Redis to find all keys matching the pattern (pattern*) and deletes them. EVAL is used to execute the Lua script, where redis.call(‘keys’, ‘pattern*’) fetches the keys and unpack passes them to the del command to be deleted.

deleting all keys of specific pattern

4 Best Practices for Deleting All Keys in Redis

Deleting all keys in Redis can significantly impact your data and applications. To ensure this critical operation is performed safely and effectively, it’s important to follow structured best practices. These practices will help you avoid common mistakes and protect your data throughout the process. Here are four key practices to consider before you delete all keys in Redis:

  • 🔑 Verify the Need to Delete: Confirm the necessity of deleting all keys. Consider whether this action is required to solve a specific problem or is just a routine cleanup. Evaluate the impact on your applications and ensure it’s the appropriate solution before proceeding.
  • 🚨 Backup Your Data: Always back up your data before performing deletions. Use BGSAVE to create a point-in-time snapshot of your data, which can be restored if necessary. This step is crucial to avoid permanent loss of critical information.
  • 🛑 Use FLUSHALL with Caution: Be very cautious when using the FLUSHALL command. This command wipes out all databases, so it’s vital to ensure you really intend to delete everything. Verify which database you’re connected to and confirm the scope of data you’re about to clear.
  • 🔒 Consider Security Implications: Deleting all keys can have security implications, especially in configurations where Redis is accessible over the internet. Unauthorized access to the FLUSHALL command can lead to data loss, so ensure proper security measures are in place.

Wrapping it Up

I’ve just gone through several ways to delete multiple keys in Redis, from using the simple DEL command to the comprehensive FLUSHALL. It’s important to remember to follow best practices, like double-checking if you really need to delete and always having backups to keep everything secure and intact.

If you want to expand your Redis expertise, consider exploring topics like using Redis CLI for basic key-value operations, understanding Docker’s restart policies for better Redis container management, and learning how to locate the IP addresses of Docker containers. These areas will help you manage Redis more effectively in a containerized environment and enhance your ability to troubleshoot and streamline your workflow when working with Redis in Docker.

Frequently Asked Questions

  1. How do you restore keys in Redis after accidental deletion?

    To restore keys in Redis after accidental deletion, you must have a prior backup, such as an RDB snapshot or AOF file. Redis itself does not provide an undo feature for deleted keys, so regular backups and enabling AOF with every-write durability are crucial for recovery.

  2. Are there any limitations to the number of keys you can delete at once in Redis?

    There is no built-in limit to the number of keys you can delete at once in Redis. However, deleting a large number of keys simultaneously can block the server and affect performance, as Redis deletion operations are processed in a single, uninterrupted transaction.

  3. How does key deletion in Redis affect memory usage immediately and over time?

    Immediately after keys are deleted in Redis, the memory used by these keys is freed, which may result in decreased memory usage. Over time, if Redis is configured with memory fragmentation optimizations, the memory allocator can further optimize the usage and handling of freed space.

  4. How can I limit access to deletion commands in Redis to enhance security?

    To enhance security and limit access to deletion commands in Redis, use Redis ACLs (Access Control Lists) to define who can execute specific commands. ACLs allow you to restrict sensitive commands like DEL, FLUSHDB, and FLUSHALL to authorized users only, thereby preventing unauthorized data deletions.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

How to View Linux File Timestamps? [3 Easy Methods]

Next Post

How to Encrypt Files in Linux? [6 Best Methods]

Leave a Reply

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

Read next