How to Delete Redis Keys by Pattern in Python

Redis is a powerful, in-memory data structure store, used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, and more. One common requirement when working with Redis is the need to delete keys based on a specific pattern. This can be particularly useful in scenarios where you need to clear cache entries or remove temporary data. In this post, we'll explore how to accomplish this task using Python.

The Challenge

Imagine you're working on an application that uses Redis for caching data. Over time, your cache grows, and you find yourself needing to delete keys that match a certain pattern. For example, you might want to delete all keys that start with "temp:" or end with ":cache". Redis CLI provides the KEYS command to find keys by pattern and the DEL command to delete keys. However, when working within a Python application, you'll need a way to execute this pattern-based deletion programmatically.

The Solution

To interact with Redis in Python, we use the redis-py library, which is a solid Python client for Redis. If you haven't already installed it, you can do so by running pip install redis.

Step 1: Connect to Redis

First, you need to establish a connection to your Redis instance:

import redis

# Connect to Redis
r = redis.Redis(host='localhost', port=6379, db=0, decode_responses=True)

Make sure to replace 'localhost', 6379, and 0 with your Redis host, port, and database number, respectively.

Step 2: Find Keys by Pattern

To find keys that match a specific pattern, you can use the scan_iter method. This method is preferred over the keys method for performance reasons, especially when working with a large number of keys.

pattern = 'temp:*'
matching_keys = r.scan_iter(pattern)

This code snippet will return an iterator over all keys that start with "temp:".

Step 3: Delete the Keys

Finally, you can iterate over the matching keys and delete them:

for key in matching_keys:
    r.delete(key)

Putting It All Together

Here's the complete script that connects to Redis, finds keys by a pattern, and deletes them:

import redis

# Connect to Redis
r = redis.Redis(host='localhost', port=6379, db=0, decode_responses=True)

# Pattern to match
pattern = 'temp:*'

# Find and delete keys
for key in r.scan_iter(pattern):
    r.delete(key)

Conclusion

Deleting Redis keys by pattern in Python is straightforward with the redis-py library. By using the scan_iter method to find keys and then deleting them, you can efficiently manage your Redis keys directly from your Python application. This approach is especially useful for maintaining caches and managing temporary data in Redis. Remember to use these operations judiciously, as deleting large volumes of keys can impact the performance of your Redis instance.