sewenew / redis-plus-plus

Redis client written in C++
Apache License 2.0
1.64k stars 351 forks source link

[QUESTION] delete keys matching a pattern using redis-plus-plus #354

Closed manishkrishan closed 2 years ago

manishkrishan commented 2 years ago

I am looking a way to delete all keys matching to a pattern like "mykeys*" using redis-plus-plus. Any suggestion, I know using redis CLI it is possible.

I am using redis cluster.

sewenew commented 2 years ago

Yes, you can use the SCAN + DEL to do the job:

auto cursor = 0LL;
auto pattern = "mykeys*";
while (true) {
    std::unordered_set<std::string> keys;
    cursor = redis.scan(cursor, pattern, std::inserter(keys, keys.begin()));
    if (cursor == 0) {
        break;
    } else {
        redis.del(keys.begin(), keys.end());
    }
}

NOTE: Do not use KEYS command to match keys in production env.

Regards

sewenew commented 2 years ago

Since there's no update, I'll close this issue.

Regards