All Products
Search
Document Center

How do I perform fuzzy match on a key for a Redis database and delete the key?

Last Updated: Dec 29, 2020

Introduction

This topic describes how to use iscan and scan to search for and delete a key in apsaradb for Redis.

Background

Alibaba Cloud reminds you that:

  • Before you perform operations that may cause risks, such as modifying instance configurations or data, we recommend that you check the disaster recovery and fault tolerance capabilities of the instances to ensure data security.
  • You can modify the configurations and data of instances including but not limited to Elastic Compute Service (ECS) and Relational Database Service (RDS) instances. Before the modification, we recommend that you create snapshots or enable RDS log backup.
  • If you have authorized or submitted sensitive information such as the logon account and password in the Alibaba Cloud Management Console, we recommend that you modify such information in a timely manner.

Every 100 keys in a scan match the key that starts with me. Once the key is matched, the key is deleted. Cluster and single-replica or master-replica architecture differ in that one uses iscan and one uses scan.

  • For Redis clusters, we recommend that you use the following methods to delete matching keys.
redis-cli -h [$Addr] -a [$Password] iscan 0 0 match 'my*' count 100 |xargs redis-cli -h [$Addr] -a [$Password] del

Note:

  • [$Addr] is the address of the apsaradb for Redis instance.
  • [$Password] is the Password used to connect to the apsaradb for Redis instance.
  • We recommend that you use the following methods to delete matching keys for a single-replica or master-replica architecture:
redis-cli -h [$Addr] -a [$Password] scan 0 0 match 'my*' count 100 |xargs redis-cli -h [$Addr] -a [$Password] del

Application scope

  • ApsaraDB for Redis