This topic demonstrates how to bulk delete specified keys or keys that match a pattern in Redis.
Notes
Back up your data before you delete keys, and perform the deletion during off-peak hours.
The examples in this topic must be run from a client (redis-cli or Python) connected to your instance. They cannot be run in Data Management (DMS).
Bulk delete specified keys
Save the keys to delete in a text file, with one key per line.
Run the following command to delete the specified keys:
# Use the asynchronous UNLINK command (Redis 4.0+) to avoid blocking. If your Redis version is earlier than 4.0, replace UNLINK with DEL. cat <file> | xargs redis-cli -h <host> -p <port> -a <password> UNLINKThis command reads each key from the file and then pipes the keys to
xargs, which passes them to redis-cli for deletion withUNLINK.NoteThe
xargscommand breaks up long parameter lists into smaller segments to pass to other commands. This avoids errors caused by an overly long argument list.Parameters:
file: The path of the file that contains the keys to delete. Example:
key.txt.host: The endpoint of the Redis instance.
port: The port number of the Redis instance. Default value: 6379.
password: The password used to connect to the Redis instance. For more information, see Connect to an instance.
Sample command:
cat key.txt | xargs redis-cli -h r-bp127cu5tb*****.redis.rds.aliyuncs.com -a Test**** UNLINK
Bulk delete keys that match a pattern
Use the methods in the following table to bulk delete keys that match a pattern, such as keys with a common prefix or suffix.
Method | Use case |
(Recommended) Use | Safely delete large amounts of data in a production environment. This method is non-blocking and ensures the stability of your online services. |
Use pipeline mode for high-throughput deletion | Run commands in batches to maximize throughput and reduce network latency. This method is ideal for scenarios that require maximum deletion efficiency. |
Use | Delete small amounts of data in development or test environments, or during a complete service outage. The |
(Recommended) Use SCAN and UNLINK
This method iteratively scans for and deletes keys that match a specific pattern.
Save the following code as the
scan_and_unlink.pyscript:import redis import sys def main(argv): if len(argv) < 4: print("Usage: python scan_and_del.py host port password match") sys.exit(-1) host = argv[1] port = argv[2] password = argv[3] match = argv[4] print("host: %s, port: %s, password: %s, match: %s\n" % (host, port, password, match)) redis_cli = redis.StrictRedis(host=host, port=port, password=password) cursor = 0 rnd = 1 while rnd == 1 or cursor != 0: result = redis_cli.scan(cursor=cursor, match=match, count=1000) ret_cursor = result[0] ret_keys = result[1] num_keys = len(ret_keys) print("Deleted %d keys in round %d. Cursor: %d" % (num_keys, rnd, ret_cursor)) if 0 != num_keys: ret_unlink = redis_cli.unlink(*ret_keys) print("unlink returned: %d, keys unlinked: %s" % (ret_unlink, ret_keys)) cursor = ret_cursor rnd += 1 print("") if __name__ == '__main__': main(sys.argv)Run the following command to execute the script:
python scan_and_unlink.py <host> <port> <password> "<key>"Parameters:
host: The endpoint of the Redis instance.
port: The port number of the Redis instance. Default value: 6379.
password: The password used to connect to the Redis instance. For more information, see Connect to an instance.
key: The pattern used to match the keys you want to delete. For example,
"test*"matches all keys with thetestprefix, such astest1,test2, andtest3.The following examples show how to use wildcards for pattern matching:
w?rld: Matches a five-character string that starts withwand ends withrld, such asworld,warld, andwxrld.w*rld: Matches any string that starts withwand ends withrld, such aswrldandwoooorld.w[ae]rld: Matcheswarldandwerld, but notworld.w[^e]rld: Matchesworldandwarld, but notwerld.w[a-b]rld: Matcheswarldandwbrld.
Use pipeline mode for high-throughput deletion
This method iteratively scans for keys that match a specific pattern and then uses a pipeline to send UNLINK commands in batch. This approach significantly reduces the Round-Trip Time (RTT).
Save the following code as the
scan_and_unlink_pipe.pyscript:import redis import sys def main(argv): if len(argv) < 4: print("Usage: python scan_and_del_pipeline.py host port password match") sys.exit(-1) host = argv[1] port = int(argv[2]) # Convert port to integer password = argv[3] match = argv[4] print(f"host: {host}, port: {port}, password: {'*' * len(password)}, match: {match}\n") # After accumulating 1,000 keys, submit a DEL command through the pipeline. You can adjust this number as needed. pipeline_batch_size = 1000 redis_cli = redis.StrictRedis(host=host, port=port, password=password, decode_responses=True) cursor = 0 total_deleted_count = 0 keys_to_delete = [] print("Starting key scan...") # Use a 'while True' loop and exit when the cursor is 0. while True: # 1. Scan a batch of keys. cursor, keys = redis_cli.scan(cursor=cursor, match=match, count=1000) if keys: # 2. Collect the scanned keys into a list for deletion. keys_to_delete.extend(keys) # 3. When the deletion list reaches the batch size or the scan is complete, execute the deletion. if len(keys_to_delete) >= pipeline_batch_size or cursor == 0: if keys_to_delete: print(f"Accumulated {len(keys_to_delete)} keys. Preparing to delete via Pipeline...") # --- Pipeline ----------- pipe = redis_cli.pipeline(transaction=False, shard_hint=None) pipe.delete(*keys_to_delete) result = pipe.execute() # ------------------------- deleted_in_batch = result[0] total_deleted_count += deleted_in_batch print(f" -> Pipeline executed. Deleted {deleted_in_batch} keys in this batch.") # Clear the deletion list to prepare for the next batch. keys_to_delete = [] # 4. If the scan is complete, exit the loop. if cursor == 0: break print(f"\nScan finished. Total keys deleted: {total_deleted_count}") if __name__ == '__main__': main(sys.argv)Run the following command to execute the script:
python scan_and_unlink_pipe.py <host> <port> <password> "<key>"Parameters:
host: The endpoint of the Redis instance.
port: The port number of the Redis instance. Default value: 6379.
password: The password used to connect to the Redis instance. For more information, see Connect to an instance.
key: The pattern used to match the keys you want to delete. For example,
"test*"matches all keys with thetestprefix, such astest1,test2, andtest3.The following examples show how to use wildcards for pattern matching:
w?rld: Matches a five-character string that starts withwand ends withrld, such asworld,warld, andwxrld.w*rld: Matches any string that starts withwand ends withrld, such aswrldandwoooorld.w[ae]rld: Matcheswarldandwerld, but notworld.w[^e]rld: Matchesworldandwarld, but notwerld.w[a-b]rld: Matcheswarldandwbrld.
Use KEYS and DEL
The KEYS command is a blocking operation that scans your entire keyspace. On a database with a large number of keys, this can cause high CPU usage and lead to performance degradation. Perform this operation during off-peak hours and only on databases with a very small number of keys
Run the following command to delete the keys that match the specified pattern:
redis-cli -h <host> -p <port> -a <password> KEYS "<key>" | xargs redis-cli -h <host> -p <port> -a <password> DELThis command uses KEYS with a wildcard pattern to find all matching keys and then pipes them to xargs, which passes them to DEL for deletion.
Parameters:
host: The endpoint of the Redis instance.
port: The port number of the Redis instance. Default value: 6379.
password: The password used to connect to the Redis instance. For more information, see Connect to an instance.
key: The pattern used to match the keys you want to delete. For example,
"test*"matches all keys with thetestprefix, such astest1,test2, andtest3.The following examples show how to use wildcards for pattern matching:
w?rld: Matches a five-character string that starts withwand ends withrld, such asworld,warld, andwxrld.w*rld: Matches any string that starts withwand ends withrld, such aswrldandwoooorld.w[ae]rld: Matcheswarldandwerld, but notworld.w[^e]rld: Matchesworldandwarld, but notwerld.w[a-b]rld: Matcheswarldandwbrld.
Sample command:
redis-cli -h r-bp127cu5tb*****.redis.rds.aliyuncs.com -a Test**** KEYS "keys*" | xargs redis-cli -h r-bp127cu5tb*****.redis.rds.aliyuncs.com -a Test**** DELVerify the deletion
Run the following SCAN command with the --scan option in redis-cli. Confirm that the command returns no matching keys.
# Count the number of matching keys. The expected result is 0.
redis-cli -h <host> -p <port> -a <password> --scan --pattern 'test:*' | wc -l