All Products
Search
Document Center

ApsaraDB for Redis:Batch delete keys in Redis

Last Updated:Dec 28, 2023

The DEL key command of Redis is used to delete a single key. To batch delete keys, you can combine the cat and xargs commands of Linux and the DEL command of Redis. If you want to delete keys that have the same prefix or suffix by using fuzzy match logic, we recommend that you use the SCAN and DEL commands of Redis.

Batch delete specified keys

Delete multiple specified keys

Before you run the command, write the list of keys that you want to delete to a file.

Run the cat command to read each line from the file as Redis keys. Then, use redis-cli to connect to the Redis server and run the DEL command to delete the keys. Command syntax:

cat <file> | xargs redis-cli -h <host> -a <password> DEL
Note

The xargs command can divide a long list of arguments into small chunks and then pass them as arguments to other commands. This prevents the "Argument list too long" error in Linux. You can use the command alone or together with pipe operators, redirection operators, or other commands.

Parameters:

  • file: the file that contains the keys to be deleted. Example: key.txt.

  • host: the endpoint that is used to connect to the ApsaraDB for Redis instance. For more information, see View endpoints.

  • password: the password that is used to connect to the ApsaraDB for Redis instance. For more information, see Logon methods.

Example:

cat key.txt | xargs redis-cli -h r-bp127cu5tb*****.redis.rds.aliyuncs.com -a Test**** DEL

Delete keys by using fuzzy match logic

Method 1 (recommended): Use the SCAN and DEL commands

Iteratively scan and delete the keys that meet specific conditions. Sample code:

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("del %d keys in round %d, cursor: %d" % (num_keys, rnd, ret_cursor))
        if 0 != num_keys:
            ret_del = redis_cli.delete(*ret_keys)
            print("ret_del: %d, ret_keys: %s" % (ret_del, ret_keys))
        cursor = ret_cursor
        rnd += 1
        print("")

if __name__ == '__main__':
    main(sys.argv)

Call the scan_and_del.py script that contains the preceding sample code and specify the connection information of the ApsaraDB for Redis instance and the key matching pattern to implement fuzzy deletion.

python scan_and_del.py <host> <port> <password> "<key>" 

Parameters:

  • host: the endpoint that is used to connect to the ApsaraDB for Redis instance. For more information, see View endpoints.

  • port: the port number of the ApsaraDB for Redis instance.

  • password: the password that is used to connect to the ApsaraDB for Redis instance. For more information, see Logon methods.

  • key: the keys that meet specific conditions in a database. For example, "test*" matches keys that start with test, such as test1, test2, and test3.

    Wildcard match description:

    • w?rld: matches five-character strings that start with w and end with rl, such as world, warld, and wxrld.

    • w*rld: matches any string that starts with w and ends with rl, such as wrld and woooorld.

    • w[ae]rld: matches warld and werld, but does not match world.

    • w[^e]rld: matches world and warld, but does not match werld.

    • w[a-b]rld: matches warld and wbrld.

Method 2: Use the KEYS and DEL commands

Warning
  • The KEYS command may cause high CPU utilization. We recommend that you use this command during off-peak hours.

  • If you use the KEYS command in a large database, the database performance is compromised. We recommend that you use this command when you process small amounts of data.

Run the KEYS command to perform wildcard matching for all keys that meet specific conditions, and then run the DEL command to delete the keys.

redis-cli -h <host> -a <password> KEYS "<key>" | xargs redis-cli -h <host> -a <password> DEL

Parameters:

  • file: the file that contains the keys to be deleted. Example: key.txt.

  • password: the password that is used to connect to the ApsaraDB for Redis instance. For more information, see Logon methods.

  • key: the keys that meet specific conditions in a database. For example, "test*" matches keys that start with test, such as test1, test2, and test3.

    Wildcard match description:

    • w?rld: matches five-character strings that start with w and end with rl, such as world, warld, and wxrld.

    • w*rld: matches any string that starts with w and ends with rl, such as wrld and woooorld.

    • w[ae]rld: matches warld and werld, but does not match world.

    • w[^e]rld: matches world and warld, but does not match werld.

    • w[a-b]rld: matches warld and wbrld.

Example:

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**** DEL

Verify whether the specified keys are deleted

redis-cli -h <host> -a <password> KEYS "<key>"