Skip to content

Instantly share code, notes, and snippets.

@Rpsl
Last active Dec 2, 2019
Embed
What would you like to do?
php script for delete keys by pattern from redis cluster
<?php
$hosts = [
'host1:port1',
'host2:port2',
];
foreach($hosts as $host){
list($host, $port) = explode(':', $host);
echo sprintf("-- %s:%d \n", $host, $port);
$redis = new Redis();
$redis->connect($host, $port);
$it = null;
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
$i = 0;
do {
$keys = $redis->scan($it, 'key:mask:*', 1000);
foreach ($keys as $key) {
$i++;
$redis->del($key);
// echo sprintf("%s \n", $key);
}
usleep(200);
} while($it > 0);
echo sprintf("removed %d keys \n", $i);
unset($redis);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment