Noman Ali
Noman Ali

Reputation: 3340

Delete Redis Keys matching a pattern Laravel 5.7

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.

Upvotes: 10

Views: 23274

Answers (6)

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

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 runs 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:*'));

Upvotes: 4

Abdessamad Zenasni
Abdessamad Zenasni

Reputation: 1

In my case using Laravel 8.x and "predis/predis": "^2.1", I have noticed that the package added a prefix "laravel_database_" so I had to remove it everytime I delete Redis hashes, I achieved that like so:

function DeleteRedisKeys($hash)
    {
            $prefix = 'laravel_database_';
            $keys = Redis::keys($hash . '*');

            $keys = array_map(function ($h) use ($prefix) {
                return str_replace($prefix, '', $h);
            }, $keys);

            Redis::del($keys);
    }

Upvotes: 0

vlauciani
vlauciani

Reputation: 1100

With Laravel8 I use:

    public function handle()
    {
        $this->call('queue:flush');

        $arrayFailed        = Redis::connection('horizon')->keys('failed:*');
        $arrayFailedJobs    = Redis::connection('horizon')->keys('failed_jobs');
        $arrayToRemove      = array_merge($arrayFailed, $arrayFailedJobs);

        $arrayMap = array_map(function ($k) {
            return str_replace(config('horizon.prefix'), '', $k);
        }, $arrayToRemove);
        Redis::connection('horizon')->del($arrayMap);

        $this->line('');
    }

Upvotes: 0

Moode Osman
Moode Osman

Reputation: 2013

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

Upvotes: 3

Nick
Nick

Reputation: 546

We can use the array_map function to go over all the keys in redis and do delete.

$redis = new Redis;
$prefix = $redis->getOption(Redis::OPT_PREFIX);
$redis->delete(array_map(
    function ($key) use ($prefix) {
        return str_replace($prefix, '', $key);
    }, $redis->keys('*'))
);

Upvotes: 1

freeek
freeek

Reputation: 978

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.

Upvotes: 24

Related Questions