Home> Database> Redis> body text

How to delete keys in batches in redis

Release: 2020-04-22 09:04:33
forward
12918 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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 "
Copy after login

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
Copy after login

Will return the number of successfully deleted items

(integer) 2
Copy after login

Keys with spaces need to be enclosed in quotation marks

DEL ”vip user mark" "vip user mary" (integer) 2
Copy after login

For more redis knowledge, please pay attention toredis 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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!