There are some keys in my database, abc1, abc2, abc3... I can use fuzzy matching keys abc to query them all, but when deleting, I need to execute the del key* command one by one. Is there any fuzzy matching to delete command?
Comment From: ShooterIT
del command supports multi keys, but doesn't support keys pattern, so you can use del/unlink abc1 abc2 abc3 ... rather than del abc1 , del abc2 , del abc3 , but please notice that deleting too many keys in one command also may freeze server.
Comment From: ffbh123456
@ShooterIT Thank you, this approach is better than deleting one by one, but it also needs to execute multiple commands, which is not convenient, I need to execute the KEYS first, and then execute the DEL according to the return value. By the way, Why not add a fuzzy delete operation? This command may look like the following: DELS abc*
Comment From: jiekun
It should be easy to finish with a Lua script or an external module.
BTW I think since the KEYS command is blocking and not recommended, it should be the same for DELS command (If exists). So that's the reason why Redis maintainers don't want to implement more commands which will block the main loop? WDYT
Comment From: zuiderkwast
The keys in Redis are stored in a hash table, so wildcard matching on keys is very inefficient in Redis. The KEYS command does a loop over all the keys in the database. Therefore, as @2014BDuck says, it's not recommended and the Redis maintainers will not add more commands which would need to loop over all the keys.
Instead of searching by wildcard, you should structure the data, for example by storing everything related to "abc" in a hash under the key "abc": HSET abc 1 one 2 two 3 three. Then you can lookup individual values as HGET abc 1 and delete them all using DEL abc.
Comment From: madolson
Scan is recommended replacement. We could build an iterative scan that slowly deletes the keys that match a pattern. The solution that zuiderkwast mentioned is probably the best option though.
Comment From: ffbh123456
Well, actually I just want to quickly delete some data in the test environment, not in the production environment to use this function