Redis: deleting many keys with pattern matching using bash shell + redis-cli
Tags: redis, bash, developer, nosql
Published: 2023-05-02 08:08:51

Have you ever had the need to delete many keys in a Redis server? If you want to do that, and you don't want to write code (python, c#, java, etc.), you can use the script described in this post.

# What do you need?

- A bash shell
- Redis installed with authentication enabled. 
- Redis-CLI

I assume that you have a basic knowledge of [Redis](https://redis.io) and [Redis CLI](https://redis.io/docs/ui/cli/) and **Bash scripting**.

# The reason why I made the script

Sometimes you can have the need to remove a set of keys that match with a specific pattern in your Redis server (e.g. "mypattern*", "\*mypattern\*", "users*", etc.). To reach this goal you can develop a solution in some language (python, c#, java, etc.) or, alternatively, you can use **Redis CLI**. The script uses the second strategy. 

# How the script works

The script uses **Redis CLI**, running two commands:
- [KEYS](https://redis.io/commands/keys/), to get all keys match with the input pattern
- [DEL](https://redis.io/commands/del/), to delete the keys (one at once).

The script read before all the keys matching the pattern using the command **Redis CLI KEYS** and then, it runs the command **Redis CLI DEL** to remove them. The script, to pipe each key to **Redis CLI DEL** command, use [xargs](https://en.wikipedia.org/wiki/Xargs), so each key can be deleted from Redis.

# The script

The script has three variables you have to set. The first two are about your installation (**host** and **port**) and the third one represents the **pattern** of keys you need to remove.

```
#!/bin/bash

#######################################################################################################
# Set your parameters
#######################################################################################################
port=6379
host="localhost"
pattern="mypattern*"
#######################################################################################################

echo -e "\nRedis @$host:$port"
echo -e "\nTrying to delete all keys with pattern: $pattern\n"

echo -e "\nEnter Redis Password : "
read -s password

keys_found=`redis-cli -p $port -h $host -a $password --raw --no-auth-warning KEYS $pattern | xargs | wc -w`

if [ "$keys_found" -eq "0" ]; then
    echo -e "\nKeys not found using pattern $pattern\n"  
    exit 1
fi

echo ""
echo "Deleting all keys with pattern $pattern"
redis-cli -p $port -h $host -a $password  --no-auth-warning KEYS $pattern | xargs redis-cli -p $port -h $host -a $password --raw  --no-auth-warning DEL
```

# Run the script

Once you've set the parameters section, run the script!
When the script starts, it'll ask the password to authenticate on your Redis server, provide the password and wait for the conclusion of the execution.

```

you@yourworkstation$./redis-keys-delete.sh

Redis @localhost:6379

Trying to delete all keys with pattern: mypattern*


Enter Redis Password : 

Deleting all keys with pattern mypattern*
2
```

# Conclusion

This simple script could be useful when you are developing, and you need to remove one or more keys from your Redis server (e.g. deployed with Docker on your workstation). The script could be a starting point to do something different, or it could be improved managing the variables as parameters, and so on. I hope this script could be helpful to someone. Suggestions and corrections are welcome.