Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to bulk delete key by redis

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

How does redis delete key in bulk? I believe there are many people do not understand, today, in order to let you know more about redis batch delete key method, so give you a summary of the following content, let's look down.

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

Bulk delete Key

There is an instruction DEL to delete a single Key in Redis, but there seems to be no instruction to delete Key in bulk, but we can do this with the help of Linux's xargs instruction.

Redis-cli keys "*" | xargs redis-cli del / / if redis-cli is not set to a system variable, you need to specify the full path of redis-cli / for example: / 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

/ / the following command specifies that the data sequence number is 0, that is, the default database redis-cli-n 0 keys "*" | xargs redis-cli-n 0 del

Delete all Key

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

/ / Delete all Key flushdb in the current database / / delete key flushall in all databases

Other forms of deletion through redis's key:

If the key contains a space image:

A log message message1

Vip user peter

Vip user mark

Vip user mary

You can delete it by adding quotation marks

DEL "a log message" DEL "vip user"

However, it is not recommended to use spaces in key. It is best to use colons to split fields.

For example, vip:user:mary

Some documents are underlined, and the hump should be fine.

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

DEL key1 key2

The number of successful deletions will be returned

(integer) 2

Key with spaces needs to be enclosed in quotation marks

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

About how redis batch delete key to share here, I hope that the above content can have a certain reference value for you, you can learn to apply. If you like this article, you might as well share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 297

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report