I have been searching and tried multiple solution but could got any helping results, I want to clear/delete all keys matching pattern products:*
.
Following are the things i have tried.
Redis::del('products:*');
Redis::del('*products:*');
Redis::del('*products*');
But nothing worked.
It is deleting key if i provide exact key name like : Redis::del('products:2:3:45');
Key are being generated like this: products:1:4:45
I have read documentation but could find anything regarding my query.
Please help.
You can't delete by pattern. But you can get all the keys by this pattern and then delete them:
Redis::del(Redis::keys('products:*'));
See more here.
I read somewhere that you cannot delete based on wildcard, you need to give the keys explicitly.
There is still a way to grab all keys and then run delete on those keys. I do it using cli like this:
redis-cli KEYS "products:*" | xargs redis-cli DEL
It fetches all the keys that match the query and run DEL
on them.
You can execute this command from Laravel.
In Laravel, fetch all keys and run delete on them using
Redis::del(Redis::keys('products:*'));
With Laravel 8 it wasn't deleting cause the key has a prefix so what I did is remove the prefix before passing it to del()
$keys = Redis::keys( 'products:*' );
if ( !empty( $keys ) ){
$keys = array_map(function ($k){
return str_replace('_prefix_database', '', $k);
}, $keys);
Redis::del( $keys );
}
i hope this will be helpful for someone
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With