10

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.

1

6 Answers 6

25

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Working like a charm with Laravel Framework 9.3.0.
Not working with Laravel 10.x
5

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

1 Comment

This worked well in Laravel 7 also. Thanks!
4

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

Comments

1

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

2 Comments

Please share the source when you copy paste from another source
Please add some explanation to your answer such that others can learn from it
0

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('');
    }

Comments

0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.