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.

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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:*

Comments

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));

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.