deleting keys in a Redis database in bulk

Source: Internet
Author: User
Tags delete key redis

Delete key in bulk
There is a command DEL that removes a single key in Redis, but it seems there is no instruction to delete the key in bulk, but we can do this with the Linux xargs command.

REDIS-CLI Keys "*" | Xargs redis-cli del  // If REDIS-CLI is not set as a system variable, you need to specify  the full path of the REDIS-CLI //  Example:/opt/redis/redis-cli Keys "*" | Xargs/opt/redis/redis-cli del  

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

Redis-cli-p 6380 Keys "*" | Xargs redis-cli-p 6380 del  // port number replaced by your own

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

redis-cli-a Password Keys "*" | Xargs redis-cli-a password del// password replaced by your own

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,  which is the default database Redis-cli-n 0 Keys "*" | Xargs redis-cli-n 0 del  

The above commands can be used together, such as

// Remove Machine Reids all Key,redis with "Pro_" starting at Port 6380 are password

Remove all keys
To delete all keys, you can use the FLUSHDB and Flushall commands of Redis

// Delete all keys  in the current database flushdb   // Remove key  from all databases Flushall  

Other

1. If key contains spaces like:
A log message Message1
VIP user Peter
VIP User Mark
VIP user Mary
You can remove them by adding quotation marks
DEL "A log Message"
DEL "VIP user"
However, it is not recommended to use a space in key, preferably using a colon to split the field

such as Vip:user:mary
Some documents use underscores, and the hump should also be

2.
In addition, a Redis del can be deleted in bulk, separated by a space
DEL Key1 Key2
will return the number of successful deletions
(integer) 2
For keys with spaces, they need to be enclosed in quotation marks.
DEL "VIP user Mark" "VIP User Mary"
(integer) 2

This article references: https://www.cnblogs.com/DreamDrive/p/5772198.html

deleting keys in a Redis database in bulk

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.