753

In my Redis DB I have a number of prefix:<numeric_id> hashes.

Sometimes I want to purge them all atomically. How do I do this without using some distributed locking mechanism?

pistacchio
  • 56,889
  • 107
  • 278
  • 420
Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160
  • Hi Steve, There is some issue with my website, I have added it to my other blog http://www.mind-geek.net/nosql/redis/delete-keys-specific-expiry-time , Hope this helps. – Gaurav Tewari Dec 09 '13 at 09:41
  • 103
    This is such a common scenario that I wish the Redis team would consider adding a native command for it. – Todd Menier Dec 14 '13 at 14:45
  • Nowadays you can just do that with Lua, see below. – Alexander Gladysh Dec 14 '13 at 22:50
  • 4
    @ToddMenier Just suggested, got this reasoning back for why it will never happen: https://github.com/antirez/redis/issues/2042 – Ray Oct 02 '14 at 18:36
  • 1
    Lots of people asking related questions about how to handle a large number of keys, keys with special characters, etc. I created a separate question as we are having this problem now and I don't think the answer is posted on this question. Here is the other question: http://stackoverflow.com/questions/32890648/how-to-bulk-delete-hundreds-of-thousands-of-keys-with-special-characters-in-redi – jakejgordon Oct 01 '15 at 14:55
  • Also check this [question](https://stackoverflow.com/questions/53716223/how-to-delete-keys-matching-a-pattern-in-redis-cluster/53721482#53721482) on how to delete keys matching a pattern in Redis Cluster mode. – for_stack Dec 11 '18 at 10:04
  • I find this useful: https://rdbtools.com/blog/redis-delete-keys-matching-pattern-using-scan/ – roottraveller May 26 '20 at 13:50

32 Answers32

868

Execute in bash:

redis-cli KEYS "prefix:*" | xargs redis-cli DEL

UPDATE

Ok, i understood. What about this way: store current additional incremental prefix and add it to all your keys. For example:

You have values like this:

prefix_prefix_actuall = 2
prefix:2:1 = 4
prefix:2:2 = 10

When you need to purge data, you change prefix_actuall first (for example set prefix_prefix_actuall = 3), so your application will write new data to keys prefix:3:1 and prefix:3:2. Then you can safely take old values from prefix:2:1 and prefix:2:2 and purge old keys.

sghael
  • 1,277
  • 1
  • 15
  • 18
Ilia Kondrashov
  • 715
  • 2
  • 9
  • 14
  • 24
    Sorry, but this is not atomic deletion. Someone may add new keys between KEYS and DEL. I do not want to delete those. – Alexander Gladysh Oct 24 '10 at 00:03
  • 48
    Keys, that will be created after KEYS command will not be deleted. – Ilia Kondrashov Oct 24 '10 at 01:30
  • OK, you're right, sorry, wrong reason (5 AM here). The correct one: If the value changes between KEYS and DEL, I do not want to delete it. Furthermore, if value is changed between KEYS and DEL, I want the change to be of... "insert" kind, not "update" (if you get what I mean) -- important for hsets for example. – Alexander Gladysh Oct 24 '10 at 02:16
  • IMHO, it is better to post a new answer in such cases -- so old answer's "karma" would not harm new one. – Alexander Gladysh Oct 24 '10 at 13:29
  • Anyway, a good solution, thanks. But it requires a single read for each write I do -- a bit of overhead. Is it possible to do what I want without that overhead? – Alexander Gladysh Oct 24 '10 at 13:30
  • Thank you, i will create new answer next time. About your question - i think no. GET is extremely cheap operation in Redis, it will not be a bottleneck. – Ilia Kondrashov Oct 24 '10 at 21:03
  • 6
    I just needed to clear out some bad keys, so Casey's first answer was spot on, except I had to move keys outside of the quotes: redis-cli KEYS "prefix:*" | xargs redis-cli DEL – jslatts Apr 07 '11 at 15:10
  • 29
    The first answer also helped me out. Another variant if your redis keys contain quotes or other characters that mess up xargs: `redis-cli KEYS "prefix:*" | xargs --delim='\n' redis-cli DEL` – overthink Sep 16 '11 at 13:31
  • 23
    If you have multible databases (keyspaces) then this is the trick: Lets say you need to delete keys in db3: `redis-cli -n 3 KEYS "prefix:*" | xargs redis-cli -n 3 DEL` – Christoffer May 19 '13 at 18:08
  • If KEYS returns a substantial amount of keys, I believe xargs will fail. See http://www.in-ulm.de/~mascheck/various/argmax/ – c0dem4gnetic Feb 24 '14 at 09:58
  • Just ran this against around 80,000 keys with no problem. Can also confirm that it didn't delete new keys created during that time. – hellosmithy Mar 12 '14 at 12:44
  • 4
    I have keys with embedded spaces. I was able to use sed to quote the keys: `redis-cli KEYS "prefix:*" | sed 's/\(.*\)/"\1"/' | xargs redis-cli DEL` – Guy Apr 29 '14 at 07:10
  • 2
    Is there an equivalent to `redis-cli KEYS "prefix:*" | xargs redis-cli DEL` for windows cmd? – Johnny5 Oct 10 '14 at 13:44
  • The only option that comes to my mind is cygwin, or, which is even better - try vagrant environment for your project, like https://puphpet.com/ – Ilia Kondrashov Jan 02 '15 at 23:41
  • 3
    For other host with selected db : ```redis-cli -h host -n db KEYS "prefix1:prefix2:*" | xargs redis-cli -h host -n db DEL``` – Yatin Mistry Mar 04 '17 at 07:23
  • 1
    How would you do it with remote -h host and user and password authentication? Adding them to a -u redis://user:password@remote.service.com does not seem to work in redis-cli – Peter Poliwoda Apr 10 '20 at 11:06
  • I had to use `redis-cli keys "stats.*" | cut -d ' ' -f2 | xargs -d '\n' redis-cli DEL`; the above didnt work for me – Tommy May 10 '21 at 16:48
  • In case you are running a remote (port-forwarded) redis instance and need run the command there. Note that xargs also need to -u host... : `redis-cli -u redis://localhost:6382 KEYS "*pull-status*" | xargs redis-cli -u redis://localhost:6382 DEL` – Lior Kupers Sep 29 '22 at 08:25
  • Dealing with doctrine keys containing backslash and dollars, I had to extend to: `redis-cli KEYS 'Db_Doctrine*' | cut -f2 | sed -e's/\\/\\\\/g' | sed -e's/\$/\\$/g' | xargs -i{} redis-cli DEL "{}"` – stena Jan 24 '23 at 19:51
  • # redis-cli -n 1 --scan --pattern 'prefix:*' | xargs redis-cli -n 1 del ////------> the -n 1 means select database index 1 if you store your keys in another db index – ermya Apr 15 '23 at 10:17
  • @overthink when dealing with keys that have sensitive characters, in a Docker `redis:x.x.x-alpine` based container `xargs` does not come with the `--delim` argument. Need to use the `-I` replace argument instead: `redis-cli --raw KEYS "prefix:*" | xargs -I{} redis-cli DEL "{}"` – Martin Tovmassian May 03 '23 at 09:54
  • Is it possible to pipe the `KEYS` output to `DEL` inside the Redis shell? As opposed to adding multiple `redis-cli` prefixes from the Bash shell – Addison Klinke Aug 22 '23 at 16:31
493

Starting with redis 2.6.0, you can run lua scripts, which execute atomically. I have never written one, but I think it would look something like this

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

Warning: As the Redis document says, because of performance maters, keys command should not use for regular operations in production, this command is intended for debugging and special operations. read more

See the EVAL documentation.

Amin Shojaei
  • 5,451
  • 2
  • 38
  • 46
mcdizzle
  • 5,205
  • 1
  • 14
  • 4
  • 28
    Important note: this fails if you have more than a couple thousand keys matching the prefix. – Nathan Osman Aug 08 '14 at 16:42
  • 112
    This one is working for big number of keys: `EVAL "local keys = redis.call('keys', ARGV[1]) \n for i=1,#keys,5000 do \n redis.call('del', unpack(keys, i, math.min(i+4999, #keys))) \n end \n return keys" 0 prefix:*` – sheerun Aug 19 '14 at 23:12
  • 252
    Ouch... redis is used a lot as simple key/store cache. This seems `del prefix:*` should be a fundamental operation :/ – Ray Oct 02 '14 at 17:58
  • 8
    @Ray frankly, if you need that feature you should simply partition the data by numetic database or server, and use flush / flushdb – Marc Gravell Oct 26 '14 at 16:45
  • 3
    @MarcGravell I heard that multiple DB's on a single redis deploy is being deprecated. To partition without functionaly this you'd need several redis servers running. Not sure adding infrastructure real or virtual to maintain would be a better option than if an efficient `del prefix:*` existed. – Ray Oct 28 '14 at 13:10
  • @Ray It isn't being *deprecated* - but: it isn't supported in "cluster". As I understand it, it is remaining a "thing" in "server". But meh; spinning up redis-server nodes is cheap too. If you think the `del {pattern}` feature should exist, maybe take that discussion to the redis-db list on google. – Marc Gravell Oct 28 '14 at 15:13
  • 1
    @MarcGravell there are a number of use cases that are precluded by partitioning, such as set operations that involve sets in different dbs (e.g. intersection). See "Disadvantages of Partitioning" here for other examples: http://redis.io/topics/partitioning – Ariel Allon Dec 02 '14 at 17:17
  • 2
    Another option: add keys that should be deleted together to a hash/list/set and delete them at once, as one object. – fatal_error Jan 28 '15 at 20:53
  • @sheerun, perhaps not returning the keys is better for many applications. If we really have so many of them, it may end up sending back tens or hundreds of megabytes. – Sergey Orshanskiy May 21 '15 at 23:17
  • Any Lua-based solution will violate the semantics of `EVAL` since it doesn't specify in advance the keys that it will operate on. It should work on a single instance but don't expect it to work with Redis Cluster. – Kevin Christopher Henry Dec 12 '15 at 05:17
  • This script fails if `keys prefix:*` returns 0 – schaffe Sep 01 '16 at 08:35
  • 15
    Yes it fails if no key matches pattern. To fix that I added a default key: `EVAL "return redis.call('del', 'defaultKey', unpack(redis.call('keys', ARGV[1])))" 0 prefix:*` – manuelmhtr Dec 01 '16 at 15:27
  • 5
    To prevent empty match error: `EVAL 'local keys = redis.call("keys", ARGV[1]); return #keys > 0 and redis.call("del", unpack(keys)) or 0' 0 prefix:*` – Sean C. Jan 19 '18 at 09:38
  • 2
    But this uses KEYS? The docs say we are not supposed to use this in production. – nickdnk May 20 '18 at 18:58
  • 1
    This works mostly - but it fails if a key has a single quote in it, because xargs complains. – xgretsch Sep 02 '20 at 07:55
89

Here's a completely working and atomic version of a wildcard delete implemented in Lua. It'll run much faster than the xargs version due to much less network back-and-forth, and it's completely atomic, blocking any other requests against redis until it finishes. If you want to atomically delete keys on Redis 2.6.0 or greater, this is definitely the way to go:

redis-cli -n [some_db] -h [some_host_name] EVAL "return redis.call('DEL', unpack(redis.call('KEYS', ARGV[1] .. '*')))" 0 prefix:

This is a working version of @mcdizzle's idea in his answer to this question. Credit for the idea 100% goes to him.

EDIT: Per Kikito's comment below, if you have more keys to delete than free memory in your Redis server, you'll run into the "too many elements to unpack" error. In that case, do:

for _,k in ipairs(redis.call('keys', ARGV[1])) do 
    redis.call('del', k) 
end

As Kikito suggested.

Eli
  • 36,793
  • 40
  • 144
  • 207
  • 13
    The code above will tank if you have a significant number of keys (the error is "too many elements to unpack"). I recommend using a loop on the Lua part: `for _,k in ipairs(redis.call('keys', KEYS[1])) do redis.call('del', k) end` – kikito Jul 02 '13 at 16:09
  • @kikito, yes, if lua cannot grow the stack to the number of keys you want to delete (most likely due to lack of memory), you'll need to do it with a for loop. I wouldn't recommend doing this unless you have to. – Eli Jul 02 '13 at 18:35
  • 2
    Lua's `unpack` transforms a table in a "list of independent variables" (other languages call that `explode`) but the max number is not dependent on the syste memory; it's fixed in lua through the `LUAI_MAXSTACK` constant. In Lua 5.1 & LuaJIT it's 8000 and in Lua 5.2 is 100000. The for loop option is recommended IMO. – kikito Jul 03 '13 at 09:48
  • 1
    It's worth noting that lua scripting is only available from Redis 2.6 up – wallacer Oct 22 '13 at 23:15
  • 5
    Any Lua-based solution will violate the semantics of `EVAL` since it doesn't specify in advance the keys that it will operate on. It should work on a single instance but don't expect it to work with Redis Cluster. – Kevin Christopher Henry Dec 12 '15 at 05:17
82

Disclaimer: the following solution doesn't provide atomicity.

Starting with v2.8 you really want to use the SCAN command instead of KEYS[1]. The following Bash script demonstrates deletion of keys by pattern:

#!/bin/bash

if [ $# -ne 3 ] 
then
  echo "Delete keys from Redis matching a pattern using SCAN & DEL"
  echo "Usage: $0 <host> <port> <pattern>"
  exit 1
fi

cursor=-1
keys=""

while [ $cursor -ne 0 ]; do
  if [ $cursor -eq -1 ]
  then
    cursor=0
  fi

  reply=`redis-cli -h $1 -p $2 SCAN $cursor MATCH $3`
  cursor=`expr "$reply" : '\([0-9]*[0-9 ]\)'`
  keys=${reply##[0-9]*[0-9 ]}
  redis-cli -h $1 -p $2 DEL $keys
done

[1] KEYS is a dangerous command that can potentially result in a DoS. The following is a quote from its documentation page:

Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using sets.

UPDATE: a one liner for the same basic effect -

$ redis-cli --scan --pattern "*:foo:bar:*" | xargs -L 100 redis-cli DEL
Itamar Haber
  • 47,336
  • 7
  • 91
  • 117
  • 12
    Nevertheless, avoiding KEYS is definitely considered best practice, so this is a great solution wherever non-atomic deletes are feasible. – fatal_error Jan 28 '15 at 20:56
  • This worked for me; however, my keys happened to be in database 1. So I had to add `-n 1` to each `redis-cli` invocation: `redis-cli -n 1 --scan --pattern "*:foo:bar:*" | xargs -L 100 redis-cli -n 1 DEL` – Rob Johansen Mar 20 '18 at 05:11
  • Note that this does not work if your keys contain special chars – mr1031011 Nov 14 '18 at 17:43
  • Interesting and valuable find... I wonder if there's a way to quote things for xargs... – Itamar Haber Nov 15 '18 at 19:31
  • what does -L 100 do?? – Aparna Jan 30 '19 at 10:09
  • `-L number Call utility for every number non-empty lines read.` – Gavin Kwok Feb 28 '19 at 07:46
  • This fails for many cases including where pattern doesn't find anything. Created a version of this that's safer. https://gist.github.com/dk8996/1f8c92c4c5ea1b4e8997b80e826b90f4 – Dimitry Jun 02 '22 at 13:54
66

For those who were having trouble parsing other answers:

eval "for _,k in ipairs(redis.call('keys','key:*:pattern')) do redis.call('del',k) end" 0

Replace key:*:pattern with your own pattern and enter this into redis-cli and you are good to go.

Credit lisco from: http://redis.io/commands/del

abagshaw
  • 6,162
  • 4
  • 38
  • 76
randomor
  • 5,329
  • 4
  • 46
  • 68
47

I am using below command in redis 3.2.8

redis-cli KEYS *YOUR_KEY_PREFIX* | xargs redis-cli DEL

You can get more help related to keys pattern search from here :- https://redis.io/commands/keys. Use your convenient glob-style pattern as per your requirement like *YOUR_KEY_PREFIX* or YOUR_KEY_PREFIX?? or any other.

And if any of you have integrated Redis PHP library than below function will help you.

flushRedisMultipleHashKeyUsingPattern("*YOUR_KEY_PATTERN*"); //function call

function flushRedisMultipleHashKeyUsingPattern($pattern='')
        {
            if($pattern==''){
                return true;
            }

            $redisObj = $this->redis;
            $getHashes = $redisObj->keys($pattern);
            if(!empty($getHashes)){
                $response = call_user_func_array(array(&$redisObj, 'del'), $getHashes); //setting all keys as parameter of "del" function. Using this we can achieve $redisObj->del("key1","key2);
            }
        }

Thank you :)

Yashrajsinh Jadeja
  • 1,699
  • 1
  • 16
  • 21
38

You can also use this command to delete the keys:-

Suppose there are many types of keys in your redis like-

  1. 'xyz_category_fpc_12'
  2. 'xyz_category_fpc_245'
  3. 'xyz_category_fpc_321'
  4. 'xyz_product_fpc_876'
  5. 'xyz_product_fpc_302'
  6. 'xyz_product_fpc_01232'

Ex- 'xyz_category_fpc' here xyz is a sitename, and these keys are related to products and categories of a E-Commerce site and generated by FPC.

If you use this command as below-

redis-cli --scan --pattern 'key*' | xargs redis-cli del

OR

redis-cli --scan --pattern 'xyz_category_fpc*' | xargs redis-cli del

It deletes all the keys like 'xyz_category_fpc' (delete 1, 2 and 3 keys). For delete other 4, 5 and 6 number keys use 'xyz_product_fpc' in above command.

If you want to Delete Everything in Redis, then follow these Commands-

With redis-cli:

  1. FLUSHDB - Removes data from your connection's CURRENT database.
  2. FLUSHALL - Removes data from ALL databases.

For Example:- in your shell:

redis-cli flushall
redis-cli flushdb
Vishal Thakur
  • 1,564
  • 16
  • 25
29

@mcdizle's solution is not working it works only for one entry.

This one works for all keys with same prefix

EVAL "for i, name in ipairs(redis.call('KEYS', ARGV[1])) do redis.call('DEL', name); end" 0 prefix*

Note: You should replace 'prefix' with your key prefix...

efaruk
  • 882
  • 9
  • 25
16

If you have space in the name of the keys, you can use this in bash:

redis-cli keys "pattern: *" | xargs -L1 -I '$' echo '"$"' | xargs redis-cli del
Inc33
  • 1,747
  • 1
  • 20
  • 26
15

Other answers may not work if your key contains special chars - Guide$CLASSMETADATA][1] for instance. Wrapping each key into quotes will ensure they get properly deleted:

redis-cli --scan --pattern sf_* | awk '{print $1}' | sed "s/^/'/;s/$/'/" | xargs redis-cli del
Quentin S.
  • 1,462
  • 20
  • 18
  • 2
    This script works perfect, tested with more than 25000 keys. – Jordi Feb 03 '20 at 15:26
  • 2
    You could also add the single quotes in awk using this funny expression ` awk '{ print "'"'"'" $1 "'"'"'"}'` – Roberto Congiu May 21 '20 at 18:29
  • the above command works well, but with scan and pattern it was taking a lot of time to complete ( for 1600 keys ). To speed it up used: keys command redis-cli keys sf_* | awk '{print $1}' | sed "s/^/'/;s/$/'/" | xargs redis-cli del – Ankush Jul 02 '20 at 07:44
15

// TODO

You think it's command not make sense bu some times Redis command like DEL not working correct and comes to the rescue of this

redis-cli KEYS "*" | xargs -i redis-cli EXPIRE {} 1 it's life hack

vampire
  • 217
  • 2
  • 7
13

@itamar's answer is great, but the parsing of the reply wasn't working for me, esp. in the case where there are no keys found in a given scan. A possibly simpler solution, directly from the console:

redis-cli -h HOST -p PORT  --scan --pattern "prefix:*" | xargs -n 100 redis-cli DEL

This also uses SCAN, which is preferable to KEYS in production, but is not atomic.

Guitan
  • 375
  • 2
  • 4
13

I succeeded this with the simplest variant of EVAL command:

EVAL "return redis.call('del', unpack(redis.call('keys', 'my_pattern_here*')))" 0

where I replaced my_pattern_here with my value.

ttulka
  • 10,309
  • 7
  • 41
  • 52
Adrian B
  • 1,490
  • 1
  • 19
  • 31
  • 3
    This worked, but i had to use single quotes. Example: `EVAL "return redis.call('del', unpack(redis.call('keys', 'my_pattern_here*')))" 0` – Andy Dec 02 '21 at 04:13
  • For those who trying to clean but got: (error) ERR Error running script (call to ...): @user_script:1: user_script:1: too many results to unpack, try a solution from comments of the similar answer above. – Riki_tiki_tavi Apr 26 '22 at 07:31
  • Finally something that works, thanks! – Brunis Mar 29 '23 at 09:58
12

I just had the same problem. I stored session data for a user in the format:

session:sessionid:key-x - value of x
session:sessionid:key-y - value of y
session:sessionid:key-z - value of z

So, each entry was a seperate key-value pair. When the session is destroyed, I wanted to remove all session data by deleting keys with the pattern session:sessionid:* - but redis does not have such a function.

What I did: store the session data within a hash. I just create a hash with the hash id of session:sessionid and then I push key-x, key-y, key-z in that hash (order did not matter to me) and if I dont need that hash anymore I just do a DEL session:sessionid and all data associated with that hash id is gone. DEL is atomic and accessing data/writing data to the hash is O(1).

Max
  • 15,693
  • 14
  • 81
  • 131
8

Adding to this answer:

To find first 1000 keys:

EVAL "return redis.call('scan', 0, 'COUNT', 1000, 'MATCH', ARGV[1])" 0 find_me_*

To delete them:

EVAL "return redis.call('del', unpack(redis.call('SCAN', 0, 'COUNT', 1000, 'MATCH', ARGV[1])[2]))" 0 delete_me_*
Khaled AbuShqear
  • 1,230
  • 14
  • 24
7

this is the easiest way that comes to mind without using any xargs magic

pure bash!

redis-cli DEL $(redis-cli KEYS *pattern*)
Massaynus
  • 322
  • 2
  • 9
6

FYI.

  • only using bash and redis-cli
  • not using keys (this uses scan)
  • works well in cluster mode
  • not atomic

Maybe you only need to modify capital characters.

scan-match.sh

#!/bin/bash
rcli="/YOUR_PATH/redis-cli" 
default_server="YOUR_SERVER"
default_port="YOUR_PORT"
servers=`$rcli -h $default_server -p $default_port cluster nodes | grep master | awk '{print $2}' | sed 's/:.*//'`
if [ x"$1" == "x" ]; then 
    startswith="DEFAULT_PATTERN"
else
    startswith="$1"
fi
MAX_BUFFER_SIZE=1000
for server in $servers; do 
    cursor=0
    while 
        r=`$rcli -h $server -p $default_port scan $cursor match "$startswith*" count $MAX_BUFFER_SIZE `
        cursor=`echo $r | cut -f 1 -d' '`
        nf=`echo $r | awk '{print NF}'`
        if [ $nf -gt 1 ]; then
            for x in `echo $r | cut -f 1 -d' ' --complement`; do 
                echo $x
            done
        fi
        (( cursor != 0 ))
    do
        :
    done
done

clear-redis-key.sh

#!/bin/bash
STARTSWITH="$1"

RCLI=YOUR_PATH/redis-cli
HOST=YOUR_HOST
PORT=6379
RCMD="$RCLI -h $HOST -p $PORT -c "

./scan-match.sh $STARTSWITH | while read -r KEY ; do
    $RCMD del $KEY 
done

Run at bash prompt

$ ./clear-redis-key.sh key_head_pattern
plhn
  • 5,017
  • 4
  • 47
  • 47
6

A version using SCAN rather than KEYS (as recommended for production servers) and --pipe rather than xargs.

I prefer pipe over xargs because it's more efficient and works when your keys contain quotes or other special characters that your shell with try and interpret. The regex substitution in this example wraps the key in double quotes, and escapes any double quotes inside.

export REDIS_HOST=your.hostname.com
redis-cli -h "$REDIS_HOST" --scan --pattern "YourPattern*" > /tmp/keys
time cat /tmp/keys | perl -pe 's/"/\\"/g;s/^/DEL "/;s/$/"/;'  | redis-cli -h "$REDIS_HOST" --pipe
tekumara
  • 8,357
  • 10
  • 57
  • 69
5

I think what might help you is the MULTI/EXEC/DISCARD. While not 100% equivalent of transactions, you should be able to isolate the deletes from other updates.

alexpopescu
  • 9,057
  • 1
  • 20
  • 13
  • 4
    But I can't figure out how to use them here. DEL is atomic by itself (or so I think). And I can't get values from KEYS until I do EXEC, so I can't use KEYS and DEL in the same MULTI. – Alexander Gladysh Oct 24 '10 at 12:29
4

Please use this command and try :

redis-cli --raw keys "$PATTERN" | xargs redis-cli del
Suf_Malek
  • 636
  • 6
  • 10
3

This is not direct answer to the question, but since I got here when searching for my own answers, I'll share this here.

If you have tens or hundreds of millions of keys you have to match, the answers given here will cause Redis to be non responsive for significant amount of time (minutes?), and potentially crash because of memory consumption (be sure, background save will kick in in the middle of your operation).

The following approach is undeniably ugly, but I didn't find a better one. Atomicity is out of question here, in this case main goal is to keep Redis up and responsive 100% of the time. It will work perfectly if you have all your keys in one of databases and you don't need to match any pattern, but cannot use http://redis.io/commands/FLUSHDB because of it's blocking nature.

Idea is simple: write a script that runs in a loop and uses O(1) operation like http://redis.io/commands/SCAN or http://redis.io/commands/RANDOMKEY to get keys, checks if they match the pattern (if you need it) and http://redis.io/commands/DEL them one by one.

If there is a better way to do it, please let me know, I'll update the answer.

Example implementation with randomkey in Ruby, as a rake task, a non blocking substitute of something like redis-cli -n 3 flushdb:

desc 'Cleanup redis'
task cleanup_redis: :environment do
  redis = Redis.new(...) # connection to target database number which needs to be wiped out
  counter = 0
  while key = redis.randomkey               
    puts "Deleting #{counter}: #{key}"
    redis.del(key)
    counter += 1
  end
end
Spajus
  • 7,356
  • 2
  • 25
  • 26
3

I tried most of methods mentioned above but they didn't work for me, after some searches I found these points:

  • if you have more than one db on redis you should determine the database using -n [number]
  • if you have a few keys use del but if there are thousands or millions of keys it's better to use unlink because unlink is non-blocking while del is blocking, for more information visit this page unlink vs del
  • also keys are like del and is blocking

so I used this code to delete keys by pattern:

 redis-cli -n 2 --scan --pattern '[your pattern]' | xargs redis-cli -n 2 unlink 
mahdi yousefi
  • 807
  • 1
  • 9
  • 15
2

Below command worked for me.

redis-cli -h redis_host_url KEYS "*abcd*" | xargs redis-cli -h redis_host_url DEL
Sumit Saurabh
  • 1,366
  • 1
  • 19
  • 33
2

If you have spaces in your key names, this will work with MacOS

redis-cli --scan --pattern "myprefix:*" | tr \\n \\0 | xargs -0 redis-cli unlink
James
  • 460
  • 2
  • 8
  • 15
2

If we want to make sure of atom operation we can try to write a Lua script.

If your Redis version support SCAN and UNLINK which is higher than 4.0.0,I prefer to use SCAN and UNLINK instead of Key and DEL in the production environment, because Key and DEL commands might block

they can be used in production without the downside of commands like KEYS or SMEMBERS that may block the server for a long time (even several seconds) when called against big collections of keys or elements.

EVAL "local cursor = 0 repeat local result = redis.call('SCAN', cursor, 'MATCH', ARGV[1])    for _,key in ipairs(result[2]) do  redis.call('UNLINK', key)   end  cursor = tonumber(result[1]) until cursor == 0 " 0 prefix:*

We can change prefix:* as we want.

D-Shih
  • 44,943
  • 6
  • 31
  • 51
  • I'd question whether SCAN has much benefit over KEYS here since you're running it in a script that will also block until completion. Memory usage perhaps. – Matt Craig Nov 02 '22 at 05:16
1

This one worked for me but may not be atomic:

redis-cli keys "stats.*" | cut -d ' ' -f2 | xargs -d '\n' redis-cli DEL
Tommy
  • 12,588
  • 14
  • 59
  • 110
0

poor man's atomic mass-delete?

maybe you could set them all to EXPIREAT the same second - like a few minutes in the future - and then wait until that time and see them all "self-destruct" at the same time.

but I am not really sure how atomic that would be.

Chris
  • 5,788
  • 4
  • 29
  • 40
0

I support all answers related to having some tool or execute Lua expression.

One more option from my side:

In our production and pre-production databases there are thousands of keys. Time to time we need to delete some keys (by some mask), modify by some criteria etc. Of course, there is no way to do it manually from CLI, especially having sharding (512 logical dbs in each physical).

For this purpose I write java client tool that does all this work. In case of keys deletion the utility can be very simple, only one class there:

public class DataCleaner {

    public static void main(String args[]) {
        String keyPattern = args[0];
        String host = args[1];
        int port = Integer.valueOf(args[2]);
        int dbIndex = Integer.valueOf(args[3]);

        Jedis jedis = new Jedis(host, port);

        int deletedKeysNumber = 0;
        if(dbIndex >= 0){
            deletedKeysNumber += deleteDataFromDB(jedis, keyPattern, dbIndex);
        } else {
            int dbSize = Integer.valueOf(jedis.configGet("databases").get(1));
            for(int i = 0; i < dbSize; i++){
                deletedKeysNumber += deleteDataFromDB(jedis, keyPattern, i);
            }
        }

        if(deletedKeysNumber == 0) {
            System.out.println("There is no keys with key pattern: " + keyPattern + " was found in database with host: " + host);
        }
    }

    private static int deleteDataFromDB(Jedis jedis, String keyPattern, int dbIndex) {
        jedis.select(dbIndex);
        Set<String> keys = jedis.keys(keyPattern);
        for(String key : keys){
            jedis.del(key);
            System.out.println("The key: " + key + " has been deleted from database index: " + dbIndex);
        }

        return keys.size();
    }

}
Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32
Denys
  • 1,308
  • 12
  • 13
0

Ad of now, you can use a redis client and perform first SCAN (supports pattern matching) and then DEL each key individually.

However, there is an issue on official redis github to create a patter-matching-del here, go show it some love if you find it useful!

Asalle
  • 1,239
  • 19
  • 41
0

If you are using Redis version below 4 you might try

redis-cli -h 127.0.0.1 -p 26379 -a `yourPassword` --scan --pattern data:* | xargs redis-cli del

and if you are using the above 4 versions, then

redis-cli -h 127.0.0.1 -p 26379 -a `yourPassword` --scan --pattern data:*| xargs redis-cli unlink

for checking your version enter your Redis terminal by using the following command

redis-cli -h 127.0.0.1 -p 26379 -a `yourPassword

then type

> INFO

# Server
redis_version:5.0.5
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:da75abdfe06a50f8
redis_mode:standalone
os:Linux 5.3.0-51-generic x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:7.5.0
process_id:14126
run_id:adfaeec5683d7381a2a175a2111f6159b6342830
tcp_port:6379
uptime_in_seconds:16860
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:15766886
executable:/tmp/redis-5.0.5/src/redis-server
config_file:

# Clients
connected_clients:22
....More Verbose
ANIK ISLAM SHOJIB
  • 3,002
  • 1
  • 27
  • 36
-1

If you use windows environment please follow this steps and it will definitely works:

  1. Download GOW from here - https://github.com/bmatzelle/gow/wiki (because xargs command doesn't works in windows)

  2. Download redis-cli for Windows (detailed explanation is here - https://medium.com/@binary10111010/redis-cli-installation-on-windows-684fb6b6ac6b)

  3. Run cmd and open directory where redis-cli stores (example: D:\Redis\Redis-x64-3.2.100)

  4. if you want to delete all keys which start with "Global:ProviderInfo" execute this query (it's require to change bold parameters (host, port, password, key) and write yours, because of this is only example):

    redis-cli -h redis.test.com -p 6379 -a redispassword --raw keys "Global:ProviderInfo*" | xargs redis-cli -h redis.test.com -p 6379 -a redispassword del

Avtandil Kavrelishvili
  • 1,651
  • 3
  • 27
  • 37
-4

Spring RedisTemplate itself provides the functionality. RedissonClient in the latest version has deprecated the "deleteByPattern" functionality.

Set<String> keys = redisTemplate.keys("geotag|*");
redisTemplate.delete(keys);
Arijeet Saha
  • 1,118
  • 11
  • 23
  • 2
    I updated Redisson sample code. Your code is not in atomic approach like Redisson does. There are new keys could appear between `keys` and `delete` methods invocations. – Nikita Koksharov Nov 30 '15 at 17:29