How to delete all keys in Redis matching pattern from within redis-cli repl?
Asked Answered
C

4

6

I am trying to delete a bunch of keys matching a prefix using redis-cli.

I have been researching how to do this online and the most common suggestion I have seen is to do it straight from the command line, like this:

$ redis-cli [options] KEYS "prefix:*" | xargs redis-cli [options] DEL

However, I would prefer to do this inside the redis-cli tool so that I do not have to pass the hostname, port, and auth parameters in the cli command every time I want to delete keys that match a pattern. So far I have tried:

  • DEL "prefix:*"
  • DEL KEYS prefix:*
  • DEL KEYS "prefix:*"
  • KEYS "prefix:*" | DEL
  • KEYS "prefix:*" DEL

Is there a way to delete all keys under a prefix from within the redis-cli tool? Is the command line the only way to achieve this?

Feel free to comment if you would like me to clarify more.

Cultivated answered 1/3, 2018 at 17:41 Comment(0)
P
11

Run this command inside redis-cli :

EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 prefix:*

Replace prefix:* with your required pattern. The output will be the number of keys deleted.

Pantoja answered 20/11, 2019 at 11:50 Comment(0)
P
2

You can write a lua script to delete keys.

local matches = redis.call('KEYS', ARGV[1])

local result = 0
for _,key in ipairs(matches) do
    result = result + redis.call('DEL', key)
end

return result 

Save this file locally as delete.lua and run it like so:

$redis-cli script load "$(cat delete.lua)"

"efe4f6a74ff9baba16d039f892dd09174b9f5777"

That "$(cat delete.lua)" just turns our script into a quoted argument. The important bit is the number that comes back (its in hex). It's the SHA1 signature of the script. We can use this to invoke the script using the EVALSHA command inside redis-cli like this:

EVALSHA efe4f6a74ff9baba16d039f892dd09174b9f5777 1 nil prefix:*
Pyrogallol answered 1/3, 2018 at 19:25 Comment(0)
L
0

Use RedisCacheWriter.clean(cacheName, keyPattern) in spring-data-redis-2.7.16, that using SCAN(10/20) + DEL(allMatchKeys) redis commands.

RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(
                redisConnectionFactory, BatchStrategies.scan(10));
redisCacheWriter.clean(cacheName, keyPattern.getBytes(StandardCharsets.UTF_8));
Lawley answered 17/12, 2023 at 16:22 Comment(0)
K
0

If you're getting an error because there are too many keys that match your prefix, you can run the following script (which uses a Lua script to do the actual work, similar to @MonzurulShimul's answer).

PREFIX='your-prefix:*'

SCRIPT="local keys = redis.call('KEYS', ARGV[1])
    for i, name in ipairs(keys) do
        redis.call('DEL', name)
    end
    return keys"

redis-cli EVAL "$SCRIPT" 0 $PREFIX

Kudos to @hbasria on github - I adapted this from https://gist.github.com/hbasria/752a573ded974dc4a971. I also wrapped it in some bash commands to make it easy to set a prefix variable and run it at the command line.

Kehoe answered 13/6, 2024 at 22:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.