14

I have installed Redis in centos and I have multiple keys of redis like this,

Product:<id>:<url>

How can I delete all Product:*:* with CLI ?

Redis version : 3.2.4 [ Latest I guess ]

Thanks!

1

8 Answers 8

40

Using the redis-cli tool, you can do the following:

redis-cli --scan --pattern 'Product:*:*' | xargs redis-cli DEL
Sign up to request clarification or add additional context in comments.

8 Comments

Not too sure why that failed for me but switched to this and it worked: redis-cli keys '*' | grep Product | xargs -i redis-cli del {}
@kakoma "failed" how?
My bad @Itamar. Just realized I'd typed a wrong pattern face palm All is well. Thanks for the tip! Upvoted
No problem, I had to undergo cosmetic surgery to remove the impression of my palm from my face <- so many years doing it :)
You can also chose server with -u and database with -n parameter like redis-cli -u redis://redisserver:6379 -n 1 --scan --pattern 'Product:*:*''
|
14

Starting with redis 2.6.0, you can use LUA scripts.

You should use this variant over the SCAN | XARGS variant because it's much faster.

The following script works also for a large amount of keys.

  1. open your redis-cli
redis-cli -p somePort -a somePassword
  1. Replace somePattern with your pattern e.g. *cars* (remember it's not regex)
EVAL "for _,k in ipairs(redis.call('keys','somePattern')) do redis.call('del',k) end" 0

1 Comment

Important note: that will lock the ENTIRE SERVER during the script execution. redis.io/docs/latest/develop/programmability
6

There's no built-in command for that. You have to use the SCAN command to get all keys that match the pattern, then use the DEL command to remove these keys.

// scan from cursor 0 to get the next cursor and keys
SCAN 0 match Product:*:*
// next_cursor, Product:x1:y1, Product:x2:y2, ...
DEL Product:x1:y1 Product:x2:y2 ...
// scan from the next cursor until it return 0
SCAN next_cursor match Product:*:*

Another solution is to use a HASH to save keys of this pattern:

// set key value
HSET Products Product:<id>:<url> value
// remove a single key
HDEL Products Product:<id>:<url>
// remove all keys
DEL Products

2 Comments

Can you give me example to scan and then delete ?
@JohnFG I update the answer. For more details on SCAN, check the documentation.
3
-n <db>            Database number

shell: redis-cli -n 1 --scan --pattern prefix:* | xargs redis-cli -n 1 del

Comments

2

put all keys you want to delete inside keylist.txt file then:

cat keylist.txt | while read rediskey; do echo "Deleting $rediskey" && redis-cli -c -h 'hostname' -p XXXX -a 'XXXXXX' UNLINK $rediskey; done

Comments

0

For Windows cmd.exe command interpreter, use FOR with /F:

FOR /F %I IN ('redis-cli KEYS Product:*:*') DO redis-cli DEL %I

If you have backquote commands enabled (not likely) you might need to use backquotes instead of apostrophes.

Source: FOR /?

Comments

-1

There are several ways to do that.

  1. https://gist.github.com/ddre54/0a4751676272e0da8186 Is not recommended on production server since it is using KEYS keyword
  2. Using ioredis (https://github.com/luin/ioredis#streamify-scanning) which supports Redis >= 2.6.12 and (Node.js >= 6)

If you want to follow second example just do following steps:

  1. Install node js >=6
  2. Create folder and inside it install ioredis by running following command:

    npm install ioredis

  3. Inside that folder create redis.js file with following content

    module.exports.redisDel = function(key) {
    console.log("del started for key: ", key);
    var Redis = require("ioredis");
    
    var redis = new Redis({
        port: 6379, // Redis port
        host: "192.168.93.27", // Redis host
        family: 4, // 4 (IPv4) or 6 (IPv6)
        password: "SetCorrectPassword"
    });
    
    return new Promise((resolve, reject) => {
    
        var stream = redis.scanStream({
            // only returns keys following the pattern of "key"
            match: key,
            // returns approximately 100 elements per call
            count: 100
        });
    
        stream.on('data', function (resultKeys) {
            if (resultKeys.length) {
                console.log(resultKeys)
                redis.del(resultKeys); //from version 4 use unlink instead of del
            }
            else {
                console.log("nothing found");
            }
        });
        stream.on('end', function (resultKeys) {
            console.log("end");
            resolve()
        })
    })
    

    }

  4. Run script by passing desired key (in our case yourKey*)

node -e 'require(\"./redis\").redisDel(\"yourKey*\")'

Comments

-1

The answer suggested above by Itamar works perfectly.

but in case you need to provide authentication in your redis setup when deleting and need to select the host in which to run the del command, you can add password, port and host as follows:-

redis-cli -a {password} --scan --pattern "*{your pattern}*"  -h {hostip} -p {port}| xargs redis-cli -a {password} -h {hostip} -p {port} del

This solved my issue, hope its useful for you as well..

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.