9951 explained code solutions for 126 technologies


php-redisHow to delete keys matching pattern


$redis->delete($redis->keys('pref*'));ctrl + c
$redis

Redis object after connection

delete

deletes specified keys

keys

returns keys that match selected pattern

'pref*'

patter to match keys for deletion (in our case: keys starting with pref...)


Usage example

<?php

$r = new Redis(); 
$r->connect('127.0.0.1', 6379);

$r->set('prefa', 1);
$r->set('prefb', 1);
print_r($r->keys('pref*'));

$r->delete($r->keys('pref*'));
print_r($r->keys('pref*'));
output
Array
(
    [0] => prefb
    [1] => prefa
)
Array
(
)