Home  >  Article  >  Database  >  How to delete keys in batches in redis

How to delete keys in batches in redis

尚
forward
2020-04-22 09:04:3312984browse

Redis is a high-performance key-value database. In redis, you can use the Linux xargs command to delete keys in batches, or you can use the flushdb and flushall commands to delete all keys.

How to delete keys in batches in redis

Delete Key in batches

Redis has the instruction DEL to delete a single Key, but there seems to be no instruction to delete Key in batches, but we can use Linux’s xargs command to complete this action

redis-cli keys "*" | xargs redis-cli del  
//如果redis-cli没有设置成系统变量,需要指定redis-cli的完整路径  
//如:/opt/redis/redis-cli keys "*" | xargs /opt/redis/redis-cli del

If you want to specify the Redis database access password, use the following command

redis-cli -a password keys "*" | xargs redis-cli -a password del

If you want to access a specific database in Redis, use the following command

//下面的命令指定数据序号为0,即默认数据库  
redis-cli -n 0 keys "*" | xargs redis-cli -n 0 del

How to delete keys in batches in redis

How to delete keys in batches in redis

How to delete keys in batches in redis

Delete all Keys

To delete all Keys, you can use Redis's flushdb and flushall commands

//删除当前数据库中的所有Key  
flushdb  
//删除所有数据库中的key  
flushall

Other forms of key deletion through redis:

If the key contains spaces like:

a log message   message1

vip user     Peter

vip user           Mark

vip user                           mary

can be deleted by adding quotation marks

DEL "a log message"
DEL " vip user "

However, it is not recommended to use spaces in the key, it is best to use colons to separate fields

For example, vip:user:mary

Some documents use underscores, so the camel case should be OK

In addition, the DEL of redis can be deleted in batches, separated by spaces

DEL key1 key2

Will return the number of successfully deleted items

(integer) 2

Keys with spaces need to be enclosed in quotation marks

DEL ”vip user mark" "vip user mary"
(integer) 2

For more redis knowledge, please pay attention to redis introductory tutorialcolumn.

The above is the detailed content of How to delete keys in batches in redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete