Redis provides the DEL command to delete a single key. When multiple keys need to be deleted in batches, you can combine the cat and xargs commands of Linux with the DEL command of Redis in a Lua script for precise matching and batch deletion of keys; or combine Redis's SCAN, RANDOMKEY, KEYS, or FLUSHALL with the DEL command for fuzzy matching and batch deletion of keys.
Precise matching batch deletion of keys
You can refer to the following command syntax to combine the cat and xargs commands of Linux with the DEL command of Redis in a Lua script to perform precise match batch deletion of keys.
cat <file> | xargs redis-cli -h <host> -a <password> DEL
The description of each parameter in the command syntax is as follows in the table.
The command example is as follows.
cat del_key.txt | xargs redis-cli -h redis-cng6qap*****.redis.ivolces.com -a Pwd@123**** DEL
Fuzzy matching batch deletion of keys
You can combine Redis commands such as SCAN, RANDOMKEY, KEYS, or FLUSHALL with the DEL command to perform fuzzy matching and batch deletion of keys.
(Recommended) Fuzzy batch deletion by SCAN and DEL commands
Combine the SCAN and DEL commands in Python code to iterate and delete keys that match the criteria. The example code is as follows.
print("Usage: python scan_del.py host port password match")
print("host: %s, port: %s, password: %s, match: %s\n" % (host, port, password, match))
redis_cli = redis.StrictRedis(host=host, port=port, password=password)
while rnd == 1 or cursor != 0:
result = redis_cli.scan(cursor=cursor, match=match, count=1000)
num_keys = len(ret_keys)
print("del %d keys in round %d, cursor: %d" % (num_keys, rnd, ret_cursor))
ret_del = redis_cli.delete(*ret_keys)
print("ret_del: %d, ret_keys: %s" % (ret_del, ret_keys))
if __name__ == '__main__':
Call the above sample code scan_del.py. When calling it, you need to specify the connection information of the Redis instance and the matching rule of the key. Then, you can achieve fuzzy matching and batch deletion of keys that meet the conditions.
python scan_del.py <host> <port> <password> "match_key"
The description of each parameter in the command syntax is as follows in the table.
The command example is as follows.
python scan_del.py redis-cng6qap*****.redis.ivolces.com 6397 Pwd@123**** "del*"
Fuzzy batch deletion by KEYS and DEL commands
danger
- The KEYS command is a blocking operation. When there are a large number of keys, it may lead to high instance CPU usage and even server blocking. It is not recommended to be used directly in a production environment. If it needs to be used in a production environment, it is strongly recommended to operate when there is less data or during off-peak business periods.
- Cache for Redis disables the KEYS command by default. If you need to use this command, you need to remove the corresponding command from the default value of the disabled command parameter. Otherwise, when executing these disabled commands, an error of no permission to execute the command will occur. For specific procedures, refer to Setting instance parameters.
Use the combination of KEYS and DEL commands to fuzzy match and batch delete qualified keys. First, use wildcards in the KEYS command to fuzzy match all qualified keys, then use the DEL command to delete these keys. The command syntax is as follows.
redis-cli -h <host> -a <password> KEYS "<match_key>" | xargs redis-cli -h <host> -a <password> DEL
The description of each parameter in the command syntax is as follows in the table.
The command example is as follows.
redis-cli -h redis-cng6qap*****.redis.ivolces.com -a Pwd@123**** KEYS "del*" | xargs redis-cli -h redis-cng6qap*****.redis.ivolces.com -a Pwd@123**** DEL
Quickly fuzzy batch deletion by FLUSHALL parameter and DEL command
danger
Cache for Redis disables the FLUSHALL command by default. If you need to use this command, you need to remove the corresponding command from the default value of the disabled command parameter. Otherwise, when executing these disabled commands, an error of no permission to execute the command will occur. For specific procedures, refer to Setting instance parameters. Combine the FLUSHALL and DEL commands to quickly perform fuzzy matching and batch deletion of keys that meet the conditions. That is, first use the WITHPATTERN or WITHOUTPATTERN parameter in the FLUSHALL command to perform fuzzy match all qualified keys, and then use the DEL command to delete these keys or other keys other than these keys. The command syntax is as follows:
- Delete all keys that match the rules
redis-cli -h <host> -a <password> FLUSHALL WITHPATTERN "<match_key>"
- Delete all keys except those matching the rules
redis-cli -h <host> -a <password> FLUSHALL WITHOUTPATTERN "<match_key>"
The description of each parameter in the command syntax is as follows in the table.
The command example is as follows:
- Delete all keys that match the rules
redis-cli -h redis-cng6qap*****.redis.ivolces.com -a Pwd@123**** FLUSHALL WITHPATTERN "*yesdel"
- Delete all keys except those matching the rules
redis-cli -h redis-cng6qap*****.redis.ivolces.com -a Pwd@123**** FLUSHALL WITHOUTPATTERN "*nodel"
Low-frequency fuzzy batch deletion by RANDOMKEY and DEL commands
danger
- Since RANDOMKEY completely randomly obtains keys, it may require multiple retrievals to find keys that meet the matching rules. This method is less efficient and is suitable for business scenarios of low-frequency fuzz batch deletion of keys.
- When the number of keys in Redis is very large and the proportion of keys that meet the matching rules is relatively small, the entire command execution process may be time-consuming.
- When a Redis instance with sharding clusters enabled executes the RANDOMKEY command, it will access each shard in a round-robin manner. That is, Redis will send the RANDOMKEY command request to each shard. When the visited shard does not have a key that meets the conditions, it will return nil. This return result only means that there is no key that meets the conditions on the current shard but does not mean that there is no key that meets the conditions on other shards. Therefore, when using the RANDOMKEY command, it is recommended not to determine whether there are keys that meet the conditions based on whether the return result is nil.
Combine RANDOMKEY and DEL commands in Python code for low-frequency fuzzy batch deletion, that is, randomly retrieving keys multiple times and deleting those that match the pattern. The code example is as follows.
print("Usage: python randomkey_del_low_frequency.py host port password match")
print("host: %s, port: %s, password: %s, match: %s\n" % (host, port, password, match))
redis_cli = redis.StrictRedis(host=host, port=port, password=password)
random_key = redis_cli.randomkey()
if re.match(match, random_key.decode('utf-8')):
result = redis_cli.delete(random_key)
print("del 1 key in round %d" % rnd)
if __name__ == '__main__':
Call the above sample code randomkey_del_low_frequency.py. When calling, you need to specify the connection information of the Redis instance and the matching rule of the key. Then, fuzzy matching and batch deletion of keys that meet the conditions can be achieved.
python randomkey_del_low_frequency.py <host> <port> <password> "<match_key>"
The description of each parameter in the command syntax is as follows in the table.
The command example is as follows.
python randomkey_del_low_frequency.py redis-cng6qap*****.redis.ivolces.com 6397 Pwd@123**** "del*"
Verify whether specified key is successfully deleted
You can check whether the specified key has been successfully deleted by using the following command.
redis-cli -h <host> -a <password> KEYS "<key>"
The command example is as follows.
redis-cli -h redis-cng6qap*****.redis.ivolces.com -a Pwd@123**** "test_del"
If the result returns empty array, it means the key does not exist in Redis, indicating the key has been successfully deleted.