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!
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!
Using the redis-cli
tool, you can do the following:
redis-cli --scan --pattern 'Product:*:*' | xargs redis-cli DEL
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.
redis-cli -p somePort -a somePassword
*cars*
(remember it's not regex)EVAL "for _,k in ipairs(redis.call('keys','somePattern')) do redis.call('del',k) end" 0
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
-n <db> Database number
shell: redis-cli -n 1 --scan --pattern prefix:* | xargs redis-cli -n 1 del
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
There are several ways to do that.
If you want to follow second example just do following steps:
Create folder and inside it install ioredis by running following command:
npm install ioredis
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()
})
})
}
Run script by passing desired key (in our case yourKey*)
node -e 'require(\"./redis\").redisDel(\"yourKey*\")'
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 /?