You can use the xargs command of Linux and the DEL command of Redis to delete multiple target keys. This topic describes the operations in details.

The xargs command is used in the Linux operating system. The command passes a list of parameters to other commands in segments. Therefore, the system does not need to process a long list of parameters and can avoid related issues. The command can be used independently, or used with pipeline operators, relocation operators and other commands.

Cautions

  • The KEYS command may cause high CPU usage. Use this command during off-peak hours.
  • If you use the KEYS command in a large database, the command affects the database performance. We recommend that you use this command in a database that contains small amounts of data.

Procedure

  1. Install ApsaraDB for Redis in the Linux operating system.
    yum install redis
  2. Run the following command to delete multiple target keys in a database. In the following figure, "test *" contains multiple target keys, such as test1, test2, and test3.
    redis-cli -h <host> -a <password> keys "<key>" | xargs redis-cli -h <host> -a <password> del 
    • <host>: the connection address of the ApsaraDB for Redis instance.
    • <password>: the password of the ApsaraDB for Redis instance.
    • "<key>": the target key such as "test" in a database.
      Note The system matches a target key in the following ways.

      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.



  3. Run the following command to check whether the system has deleted the keys that contain test.
    redis-cli -h <host> -a <password> keys "*"
    • <host>: the connection address of the ApsaraDB for Redis instance.
    • <password>: the password of the ApsaraDB for Redis instance.