CodeIgniter4 icon indicating copy to clipboard operation
CodeIgniter4 copied to clipboard

Bug: deleteMatching() throws "ERR wrong number of arguments for 'del' command" on empty result set in PredisHandler

Open mbnl opened this issue 4 weeks ago • 0 comments

PHP Version

8.4

CodeIgniter4 Version

4.6.3

CodeIgniter4 Installation Method

Manual (zip or tar.gz)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

apache

Database

No response

What happened?

When using cache()->deleteMatching('user_'. auth()->id() . '_*'); with Redis, if there are no matching keys, Redis throws the following error: ERR wrong number of arguments for 'del' command

The issue occurs because CodeIgniter\Cache\Handlers\PredisHandler::deleteMatching() sends an empty array to Redis without checking if there are any matched keys.

Steps to Reproduce

  1. Use Redis as cache in a CodeIgniter 4 project.
  2. Call cache()->deleteMatching('user_'. auth()->id() . '_*'); where no keys match the pattern.
  3. Observe the error returned by Redis.

Expected Output

The method should return true or behave gracefully without sending an empty array to Redis, avoiding the error.

Anything else?

A simple fix is to add a check in deleteMatching() to return early if no keys are matched:

` public function deleteMatching(string $pattern) { $matchedKeys = [];

foreach (new Keyspace($this->redis, $pattern) as $key) {
    $matchedKeys[] = $key;
}

if (empty($matchedKeys)) {
    return true;
}

return $this->redis->del($matchedKeys);

} `

This prevents Redis from receiving an empty del command and resolves the issue.

mbnl avatar Dec 10 '25 07:12 mbnl