redis steps to delete key in batches

  • 2020-09-28 09:16:38
  • OfStack

There are millions of useless key instances on a server due to the misuse of plug-ins. In order to delete the useless data, I searched redis's method of batch deleting key on the Internet and found that there were problems in the process of using key. After my research, I finally found the correct usage of redis to delete key in batches.

This article shares the latest version of Redis's methods to delete key in batches, hoping to help users with the same problem.

redis bulk delete key

The redis bulk delete key command given by many articles and tutorials on the web is:


redis-cli KEYS "$PATTERN" | xargs redis-cli DEL

In my practice, there are two problems with this command:

1. The result of ES25en-ES26en KEYS "$PATTERN" appears as a number instead of a pure key list, as shown below:


[root@node1]# redis-cli keys "*"
1) ":default:is_blog_installed"
2) ":site-options:1-notoptions"

I used version 5 of Redis. I did not test whether the Numbers like 1) and 2) will be added in the lower version of Redis. It can be thought that because of the number, DEL deleted after the pipeline is the wrong key.

2. If key has Spaces, the DEL after the pipe will not be deleted correctly. For example, key is "123 4566". When it is sent to the back of the pipeline to be deleted, it becomes the deletion of two key, which is not consistent with the expectation.

After 1 study, the correct command of redis to delete key batch is:


redis-cli --raw KEYS "$PATTERN" | xargs -I {} redis-cli DEL "{}"

There are two key points to solve the problems existing in the above orders respectively:

The raw parameter is used to remove the result number. xargs USES placeholders to pass the entire result when key is deleted, avoiding the whitespaces problem.

The corrected command worked fine in my case, which was to delete more than 2 million key, which was a bit slow and took more than half an hour.

redis Move key in batches

If you know the mode of USING key and the number of key is very small, you can move key in batches through redis, and then quickly delete the method of flushdb. The efficiency is much higher than the above mentioned method of deleting a large number of useless key in batches.

The operation method is as follows:

Batch Mobile key: redis-cli --raw KEYS "$PATTERN" | xargs -L1 -I{} redis-cli MOVE {} 1 Where the last 1 of the command is the number of the backup database; Empty the current database: redis-cli flushdb ; Restore key: redis-cli -n 1 --raw KEYS "$PATTERN" | xargs -L1 -I{} redis-cli MOVE {} 0 .

Note that the command to clear the current database is flushdb And don't use flushall . flushdb and flushall The difference is: flushdb Will only empty the current database of data, and flushall Clears out all data for the current instance of redis.

redis - cli parameters

Use of the above command redis-cli Tool complete, operating on the default database numbered 0. For passwords, remote redis hosts, redis-cli There are the following useful parameters:

[

-ES99en: Database of operations;

-ES102en: redis database password

-ES106en: redis host ip, via -ES109en you can remotely delete key in batch

-ES113en: redis port

]

Please refer to the help documentation for more parameters.

The above is redis batch delete key steps detailed content, more about redis batch delete key information please pay attention to other related articles on this site!


Related articles: