--scan --pattern *some_numbers* couldn't find most of my keys - redis

My redis store keys that looked like this
behavior:5c3c841c6efcbd1a6c066b48#20210125
behavior:5ea04f8b3d79784a29e02d37#20210110#submissionCount
kelas:5c73be3be611a06042041f0a#behavior:5f9a70b8c7f67353e8cd5969#20210104#studentCount
student:5c497fdd422c7d511918cc78#behavior:5c49114902b1de4a801b556b#20210123
teacher:5ef1c48fa4fee10f9817b5b2#20210126
But now I want to delete some of the keys. So I thought running this
redis-cli -n 1 --scan --pattern *202101* | xargs redis-cli unlink
will delete all the keys listed above, since all the keys above have 202101 inside it. But I was wrong. Is there any way I can delete my keys by supplying pattern such as above?

Related

Redis delete batch keys strips slashes from key names

In my MacOs 10.15 environment I have a strange Redis behaviour when I list some keys with:
redis-cli -n 1 --scan --pattern "product_doctrine*"
It gives me for example:
product_doctrine[AppBundle\Entity\ColumnMapping\$GEDMO_TIMESTAMPABLE_CLASSMETADATA][21546]
But It doesn’t delete it using xargs:
redis-cli -n 1 --scan --pattern "product_doctrine*" | xargs -L 1 redis-cli -n 1 del
(integer) 0
Is it maybe of the key name having special characters? Because if I run this inide the redis-cli:
SCAN 0 MATCH product_doctrine*
it shows the keys with escaped antislash:
"product_doctrine[AppBundle\\Entity\\ShopSettings\\$GEDMO_SOFTDELETEABLE_CLASSMETADATA][11677]"
Inside redis-cli I can delete those kind of keys successfully with
127.0.0.1:6379[1]> del "product_doctrine[AppBundle\\Entity\\ShopSettings\\$GEDMO_SOFTDELETEABLE_CLASSMETADATA][11677]"
(integer) 1
On macOS 10.14 the following does it:
redis-cli --scan --pattern "foo*" | sed 's/\\/\\\\/' | xargs -L 1 redis-cli DEL
¯\_(ツ)_/¯

xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

I'm trying to use redis-cli w/ xargs to delete keys matching my pattern, yet getting following error:
root#543b9f09e6a8:/data# redis-cli -n 0 --scan --pattern ISO:* | xargs redis-cli -n 0 DEL
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
(integer) 0
root#543b9f09e6a8:/data#
Please advise.
Thanks in advance!

How to get value by redis-cli keys

I want get value by redis-cli keys
This is work
redis-cli keys number_* | xargs redis-cli del
But this is not work
redis-cli keys number_* | xargs redis-cli get
The difference between DEL and GET, in this context, is that the former is variadic (i.e. accepts one or more arguments) whereas the latter isn't (one and only one key name is expected).
To solve this you can choose one of the following:
Use the -L switch with xargs, i.e.: redis-cli keys number_* | xargs -L 1 redis-cli get
Use MGET, i.e.: redis-cli keys number_* | xargs redis-cli mget
Important warning: KEYS is a dangerous command as it may block the server for a long time - do not use it in production!

Why do I get "(error) ERR unknown command: redis-cli" when in the REDIS console?

I am trying to delete some keys but cannot execute any redis-cli commands:
redis-cli --scan --patter 'assetInfo*' | xargs redis-cli del
The error is:
(error) ERR unknown command: redis-cli
I am using REDIS version 3.2.7. Does this version not support redis-cli?
What gives?
Update: When I do this without 'redis-cli' I get this:
Azure Redis Health Dev:0>--scan --pattern 'spout*' | xargs redis-cli del
ERR unknown command: --scan
The error you're getting is a Redis error, which means you're already connected (and probably inside the cli). redis-cli is a command line (i.e. shell) utility for opening a connection to Redis and running commands.
P.S. your --pattern switch is missing an "n"

redis keys command delete data but scan command does not

redis-cli -s /data/redis/redis.sock --scan --pattern "*abcd|6128*" | xargs -L 100 redis-cli -s /data/redis/redis.sock DEL
above command is not deleting adta from redis and giving following output
(integer) 0
While the keys command works perfectly
redis-cli -s /data/redis/redis.sock KEYS 'abcd|6291*' | xargs redis-cli -s /data/redis/redis.sock DEL;
Is there is something wrong i am doing
Try xargs with -L 1 instead. Worked for me.
redis-cli -s /data/redis/redis.sock --scan --pattern "*abcd|6128*" | xargs -L 1 redis-cli -s /data/redis/redis.sock DEL
BTW, KEYS should be avoided in production environments as it is a blocking command.
scan only applies to some keys (by default on 10 keys per iteration). It returns an offset to continuesly run scan until you reach offset 0. Then you have sampled all keys. More details are in the documentation: http://redis.io/commands/scan
Keys on the other hand samples all keys in the db in one pass. It is also blocking due to redises single threaded architecture which might be bad for performance of other clients.