Redis: deleting many keys with pattern matching using bash shell + redis-cli

Aniello Musella - May 2 '23 - - Dev Community

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 and Redis 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, to get all keys match with the input pattern
  • 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, 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

. . . . . . . . . . . . . . .
Terabox Video Player