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
redis-cli keys '*' | grep Product | xargs -i redis-cli del {}
–
Razor -u
and database with -n
parameter like redis-cli -u redis://redisserver:6379 -n 1 --scan --pattern 'Product:*:*''
–
Haycock (error) CROSSSLOT Keys in request don't hash to the same slot
I am using following to delete pattern hi*
form cluster redis-cli -u redis://localhost:6379 --scan --pattern 'hi*' | xargs redis-cli -u redis://localhost:6379 DEL
–
Miguelmiguela 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
SCAN
, check the documentation. –
Mattern -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 /?
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..
© 2022 - 2024 — McMap. All rights reserved.