Ways to delete multiple keys from Redis cache.

Navnath Chinchore
Geek Culture
Published in
3 min readAug 23, 2021

Redis cache key can be deleted in two ways.

With Keys Command

Keys command will scan all the keys present in the Redis data base with matching pattern that is provided as input in single go. Scanning all the keys in the production Redis will take time if you have millions of keys. As all of we know Redis is single threaded and scanning all the keys from prod Redis cache will block other Redis command from running . So we need to avoid using keys command on prod Redis instances.

With Scan command with Lua script.

Since Scan command allow for incremental iteration, returning only a small number of elements per call. By default SCAN returns keys in sets of 10. If you want more or less than 10 you can specify using COUNT. Please make sure that you does not give the count value too big. If you give the count value too big then it will start working as keys command only. We can combine Scan command with Lua Script to take advantages of Lua script. Below are some advantages of using Lua script for Redis operations.

Benefits of Lua Script:

1. Reduce network overhead: the operation of the original five network requests can be completed with one request, and the logic of the original five requests can be completed on the Redis server. The use of scripts reduces network round-trip latency.

2. Atomic operations: Redis will execute the entire script as a whole without being inserted by other commands.

3. Reuse: The script sent by the client will be stored in Redis permanently, which means that other clients can reuse the script without using code to complete the same logic.

Now let’s see how we can delete the key with the both approach.

  • Via Keys Command :-

We can use keys command like below to delete all the keys which match the given patters “user*" from the Redis.

redis-cli -h {host}-p {port} — raw keys “users*” | xargs redis-cli DEL

Note :- Not recommended on production Redis instance.

  • Scan Command with Lua script to delete cache keys:-
    This

    I have written Lua script to delete multiple keys by pattern . Script uses the scan command to delete the Redis cache key in incremental iteration. Please follow below steps to delete the Redis key with pattern via Lua script with scan command.
  • Copy below Lua script on your machine and save it with .lua extension.
local cursor="0";
local count = 0;
repeat
#replace "REPLACE_ME_IAM_KEY_PATTERN" with your key pattern that you want to delete
local scanResult = redis.call("SCAN", cursor, "MATCH", "REPLACE_ME_IAM_KEY_PATTERN", "COUNT", 100);
local keys = scanResult[2];
for i = 1, #keys do
local key = keys[i];
redis.replicate_commands()
redis.call("DEL", key);
count = count +1;
end;
cursor = scanResult[1];
until cursor == "0";
return "Total "..count.." keys Deleted" ;
  • After creating file locally. Please make sure that you make changes to key pattern you want to delete. (This step is important to verify if you are deleting intended keys from Redis)
  • Please CD to folder where have you created Lua script
  • Run Lua script with the below command by substituting appropriate host.
  • Comamnd :- redis-cli -h {host} --eval Delete-Keys.lua
  • Command will return the total no of keys deleted by Lua script.
    example :- Total "100 keys Deleted"

Note: Please note that EVAL command will also block your Redis instance until completion of your lua script.

Thanks For Reading….!!!☺️

--

--

Navnath Chinchore
Geek Culture

Sr Software Engineer | Talks About coding | Knowledge Sponge | Growing at Home | Trying hard to learn new Technology.