You can run the cat or xargs command of Linux together with the DEL command of ApsaraDB for Redis to batch delete keys that meet specific conditions.

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.

Procedure

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 want to process small amounts of data.
  1. Log on to the Elastic Compute Service (ECS) instance. Then, download and install redis-cli.
    1. Run the following command to download the Redis source code package:
      wget https://download.redis.io/releases/redis-6.0.9.tar.gz
      Note In this example, Redis 6.0.9 is used to demonstrate the operations. You can install other versions. For more information, visit the Redis official website.
    2. Run the following command to decompress the Redis source code package:
      tar xzf redis-6.0.9.tar.gz
    3. Run the following command to go to the directory to which the Redis source code package is decompressed. Then, compile and install redis-cli.
      cd redis-6.0.9&&make
      The system requires 2 to 3 minutes to compile and install redis-cli.
    4. Run the following command to go to the src directory:
      cd src
  2. Delete keys that meet specific conditions from databases.
    • Delete the exact keys that you specify
      You can specify the keys that you want to delete in a file, such as key.txt, and then run the following command to delete these keys:
      cat keys.txt | xargs redis-cli -h <host> -a <password> del
      Parameters:
      • 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.
    • Delete keys by using fuzzy match logic
      Run the following command:
      redis-cli -h <host> -a <password> KEYS "<key>" | xargs redis-cli -h <host> -a <password> del
      Parameters:
      • 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.
      • key: the one or more keys that you want to delete from a database in the ApsaraDB for Redis instance. Example: "test*".
        Fuzzy match description:
        • w?rld: matches world, warld, and wxrld.
        • w*rld: matches 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.
      In the following figure, test* indicates multiple keys, such as test1, test2, and test3.
  3. Run the following command to check whether the specified keys are deleted:
    redis-cli -h <host> -a <password> KEYS "test*"
    • 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.