Normally, I get the key set then use a look to delete each key/value pair.
Is it possible to just delete all keys via pattern?
ie:
Del sample_pat
KEYS is not recommended to use due to its inefficiencies when used in production. Please see https://redis.io/commands/keys. Instead, it is better to use SCAN. Additionally, a more efficient call than repeated calls to jedis.del() is to make one single call to jedis to remove the matching keys, passing in an array of keys to delete. A more efficient solution is presented below:
Set matchingKeys = new HashSet<>();
ScanParams params = new ScanParams();
params.match("sample_pattern:*");
try(Jedis jedis = jedisPoolFactory.getPool().getResource()) {
String nextCursor = "0";
do {
ScanResult scanResult = jedis.scan(nextCursor, params);
List keys = scanResult.getResult();
nextCursor = scanResult.getStringCursor();
matchingKeys.addAll(keys);
} while(!nextCursor.equals("0"));
if (matchingKeys.size() == 0) {
return;
}
jedis.del(matchingKeys.toArray(new String[matchingKeys.size()]));
}