PHPackages                             vectorial1024/laravel-cache-evict - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. vectorial1024/laravel-cache-evict

ActiveLibrary[Database &amp; ORM](/categories/database)

vectorial1024/laravel-cache-evict
=================================

Efficiently remove expired Laravel file/database cache data

2.1.0(1mo ago)5813.2k↓11.3%5MITPHPPHP ^8.5CI passing

Since Jan 7Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/Vectorial1024/laravel-cache-evict)[ Packagist](https://packagist.org/packages/vectorial1024/laravel-cache-evict)[ GitHub Sponsors](https://github.com/Vectorial1024)[ RSS](/packages/vectorial1024-laravel-cache-evict/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (16)Used By (0)

laravel-cache-evict
===================

[](#laravel-cache-evict)

[![Packagist License](https://camo.githubusercontent.com/8ef17fee4544057bc1850573463e27e7d80a004a7a5aa622905e6e3b51cd5de5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f766563746f7269616c313032342f6c61726176656c2d63616368652d65766963743f7374796c653d706c6173746963)](https://packagist.org/packages/vectorial1024/laravel-cache-evict)[![Packagist Version](https://camo.githubusercontent.com/13b50bee16d36bd5bf4a72520c0ec53e2d443281142dab243d334684c1560a6d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f766563746f7269616c313032342f6c61726176656c2d63616368652d65766963743f7374796c653d706c6173746963)](https://packagist.org/packages/vectorial1024/laravel-cache-evict)[![Packagist Downloads](https://camo.githubusercontent.com/6ce7ee0a4e03ca4cfebb87e84a5536250bcb5238318b77dcbe593c1f0af35023/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f766563746f7269616c313032342f6c61726176656c2d63616368652d65766963743f7374796c653d706c6173746963)](https://packagist.org/packages/vectorial1024/laravel-cache-evict/stats)[![PHP Dependency Version](https://camo.githubusercontent.com/d34b5dd0209fc90abf7ecc5bd04536efb29eeb75b7b60feb9c851abfac48d9a9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f766563746f7269616c313032342f6c61726176656c2d63616368652d65766963742f7068703f7374796c653d706c6173746963266c6162656c3d504850)](https://packagist.org/packages/vectorial1024/laravel-cache-evict)[![GitHub Actions Workflow Status](https://camo.githubusercontent.com/d88e16f22bb3e437f9950e907b0c177b328cfaf075ee4121e9624d0d27dd7be0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f566563746f7269616c313032342f6c61726176656c2d63616368652d65766963742f7068702e796d6c3f7374796c653d706c6173746963)](https://github.com/Vectorial1024/laravel-cache-evict/actions/workflows/php.yml)[![GitHub Repo Stars](https://camo.githubusercontent.com/db98531955ae4e85e8d7d4d2a1deecdca7bb1715f1abb18ba3002e0cdeeebe86/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f766563746f7269616c313032342f6c61726176656c2d63616368652d6576696374)](https://github.com/Vectorial1024/laravel-cache-evict)[![GitHub Sponsors](https://camo.githubusercontent.com/2b86cdb303c515ccf3e1f9f6e6f5a0c4da487ffd74afa6560cbf4b2391ca7610/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73706f6e736f72732f566563746f7269616c313032343f7374796c653d706c6173746963)](https://github.com/sponsors/Vectorial1024)

[![GitHub Actions File Status](https://camo.githubusercontent.com/03a66a50d4804a6c654c148f73839bbfe719679e8f716cf0db0e686ef5b2253a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f566563746f7269616c313032342f6c61726176656c2d63616368652d65766963742f7068702d66696c652e796d6c3f7374796c653d706c6173746963266c6162656c3d66696c65)](https://github.com/Vectorial1024/laravel-cache-evict/actions/workflows/php-file.yml)[![GitHub Actions MariaDB Status](https://camo.githubusercontent.com/4499120468915dd84d668ae665776921312340090780840567abf2356b09595f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f566563746f7269616c313032342f6c61726176656c2d63616368652d65766963742f7068702d6d6172696164622e796d6c3f7374796c653d706c6173746963266c6162656c3d6d617269616462)](https://github.com/Vectorial1024/laravel-cache-evict/actions/workflows/php-mariadb.yml)[![GitHub Actions MySQL Inno Status](https://camo.githubusercontent.com/c9f2c8356659bc18ec0a80febf52bd7756fa579cba07388500ebe96ad75a7741/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f566563746f7269616c313032342f6c61726176656c2d63616368652d65766963742f7068702d6d7973716c2d696e6e6f2e796d6c3f7374796c653d706c6173746963266c6162656c3d6d7973716c2d696e6e6f)](https://github.com/Vectorial1024/laravel-cache-evict/actions/workflows/php-mysql-inno.yml)[![GitHub Actions MySQL LTS Status](https://camo.githubusercontent.com/ec087f5b7edf93ff5119d21fde6cfad06bc93366ebce6148a6e0d190ebc68229/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f566563746f7269616c313032342f6c61726176656c2d63616368652d65766963742f7068702d6d7973716c2d6c74732e796d6c3f7374796c653d706c6173746963266c6162656c3d6d7973716c2d6c7473)](https://github.com/Vectorial1024/laravel-cache-evict/actions/workflows/php-mysql-lts.yml)[![GitHub Actions PgSQL Status](https://camo.githubusercontent.com/f863087a7da078443c14711dc00fa28c3838ed81c4120e63f7f24cec93848da6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f566563746f7269616c313032342f6c61726176656c2d63616368652d65766963742f7068702d706773716c2e796d6c3f7374796c653d706c6173746963266c6162656c3d706773716c)](https://github.com/Vectorial1024/laravel-cache-evict/actions/workflows/php-pgsql.yml)[![GitHub Actions SQLite Status](https://camo.githubusercontent.com/45f188153e5ec554af4ff917eab36354b06f75e7901aa8fe27a3526fcff776c7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f566563746f7269616c313032342f6c61726176656c2d63616368652d65766963742f7068702d73716c6974652e796d6c3f7374796c653d706c6173746963266c6162656c3d73716c697465)](https://github.com/Vectorial1024/laravel-cache-evict/actions/workflows/php-sqlite.yml)[![GitHub Actions Sql Server Status](https://camo.githubusercontent.com/2e787dc07caea94b8189226cfa46d921a16c54ebcb69289a96fb97a17eba0ba4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f566563746f7269616c313032342f6c61726176656c2d63616368652d65766963742f7068702d73716c7372762e796d6c3f7374796c653d706c6173746963266c6162656c3d73716c737276)](https://github.com/Vectorial1024/laravel-cache-evict/actions/workflows/php-sqlsrv.yml)

Efficiently remove expired cache data in Laravel.

Situation
---------

[](#situation)

As of writing, several Laravel cache drivers do not have automatic removal of expired cached items:

- `file`
- `database`

### Why is it a problem?

[](#why-is-it-a-problem)

Using any of the above cache drivers without regularly removing the expired items (aka "key eviction" in Redis) can result in storage overload, especially when you are creating a lot of temporary items with random keys.

The `cache:clear` command from Laravel works, but might not be the thing you want. It does not check item expiry (it removes everything), and also removes cached Laravel facades in `/storage/framework/cache/`, which can be especially problematic when you are using the `file` cache driver (consider a case: cache items are created by the `www-data` user but `/storage/framework/cache/*` is owned by the `ubuntu` user).

In this case, this tool can help you remove only the expired items in your cache. See below sections for more details.

This tool is designed to be memory efficient and (for `database` caches) non-blocking, so even if there are a lot of items in the cache (e.g. you are running this for the first time to deal with an oversized cache), it can still run reasonably well.

Install
-------

[](#install)

via Composer:

```
composer require vectorial1024/laravel-cache-evict
```

Suggestions:

- (Optional) `ext-intl` allows using Laravel's own `Number::fileSize()` for size reporting

### Supported cache types

[](#supported-cache-types)

The following cache drivers from `cache.php` are currently supported:

- `database`
- `file`

Some drivers (e.g. `memcached`, `redis`, `mongodb`, etc.) will never be supported because they have their own item eviction mechanisms; use those features instead of this tool!

Custom eviction strategies can be defined for other cache drivers that does not have their own eviction mechanisms (see FAQ section).

### Change log

[](#change-log)

Please see `CHANGELOG.md`.

### Version-Bumping Policy

[](#version-bumping-policy)

To address deprecations and encourage code modernization, we may rarely bump the major/minor version number to require a newer set of PHP and Laravel version.

This doesn't happen often, and most of the time, this version-bumping will not introduce breaking changes.

LibraryPHPLaravelNotes`2.2.x`8.9?16?Specuative`2.1.x``^8.5`12, 13Reach for Laravel 12 first before reaching for `2.1.x``2.0.x``^8.1`10, 11, 12NoneUsage
-----

[](#usage)

You may run this in the command line:

```
# evicts the default cache in your Laravel app
php artisan cache:evict

# you may also specify the cache to clear; e.g. the file cache defined in cache.php:
php artisan cache:evict file
```

Or, you may put this into your console kernel schedule:

```
use Vectorial1024\LaravelCacheEvict\CacheEvictCommand;

// note: because this command may have long running time, it is strongly recommended to run this command in the background
// this avoids accidentally delaying other scheduled tasks

// evicts the default cache in your Laravel app
Schedule::command(CacheEvictCommand::class)->daily()->runInBackground();

// you may also specify the cache to clear; e.g. the file cache defined in cache.php:
Schedule::command(CacheEvictCommand::class, ['file'])->daily()->runInBackground();
```

### Listening for eviction completion

[](#listening-for-eviction-completion)

Sometimes, it may be required to listen for cache eviction completion (e.g., to log some eviction stats for analytics). This tool now has built-in Laravel events to conveniently listen to cache eviction completion:

- `Vectorial1024\LaravelCacheEvict\Events\CacheEvictionCompleted`: fired when cache eviction is complete

Refer to the Laravel docs for setting up the event listener.

Note: the `CacheEvictionCompleted` event is emitted with an `AbstractEvictStrategy` object to remain general. Use the following ways to specify the exact cache store that should be listened to:

- Use `$event->evictStrategy->storeName` to check the name of the cache; or
- Use `instanceof` to type-check for specific type of cache

An example event listener `handle` function body is provided:

```
use Vectorial1024\LaravelCacheEvict\Events\CacheEvictionCompleted;
use Vectorial1024\LaravelCacheEvict\Database\DatabaseEvictStrategy;
use Vectorial1024\LaravelCacheEvict\File\FileEvictStrategy;

// ...

public function handle(CacheEvictionCompleted $event): void
{
    // abstract instance
    $cacheStrat = $event->evictStrategy;
    if ($cacheStrat instanceof FileEvictStrategy) {
        // this was an eviction on a file cache...
        return;
    }
    if ($cacheStrat instanceof DatabaseEvictStrategy) {
        // this was an eviction on a database cache...
        return;
    }
    if ($cacheStrat->storeName == 'storeName') {
        // this was an eviction on a cache named 'storeName'...
        return;
    }
    // ...
}
```

### The relationship with `cache.php`

[](#the-relationship-with-cachephp)

This tool checks the cache *name* (not *driver*!) inside `cache.php` to determine which cache to clear. This means, if you have the following `cache.php` ...

```
[
    'stores' => [
        'local_store' => [
            'driver' => 'file',
            // other config...
        ],

        'another_store' => [
            'driver' => 'file',
            // other config...
        ],
    ],
]
```

... and you run the command like this ...

```
php artisan cache:evict local_store
```

... then, you will only evict the `local_store` cache. The `another_store` cache is unaffected by this command (assuming both are using separate directories, of course).

Testing
-------

[](#testing)

PHPUnit (using `orchestra/testbench`) via Composer:

```
composer run-script test
```

The above command only runs the "core" test cases that ensures the core functionalities (e.g. selecting the correct eviction strategy) are working.

To further test vendor-specific eviction correctness (managed by PHPUnit Test Suites), have a look at the various Composer scripts. Some of them are listed below:

- `test-file`: tests for the Laravel file cache
- `test-sqlite`: tests for the Laravel database cache (using SQLite)

There are other scripts not listed here to test for e.g. MySQL and PostgreSQL, and several other vendors. See `composer.json` for more details.

It may be more convenient to rely on GitHub Workflow to test/review eviction correctness.

Frequently-asked questions (FAQ)
--------------------------------

[](#frequently-asked-questions-faq)

### How to define custom eviction strategies?

[](#how-to-define-custom-eviction-strategies)

You can do so inside your Laravel service provider. Simply do the following:

```
public function boot()
{
    // register a handler for a specific cache driver
    // YourEvictStrategy extends Vectorial1024\LaravelCacheEvict\AbstractEvictStrategy
    CacheEvictStrategies::registerDriverStrategy('your_driver_name', YourEvictStrategy::class);

    // or, register that a specific cache driver should not be handled because it has its own handler already
    CacheEvictStrategies::registerDriverRefusedBecauseFeatureExists('self_managed_driver_name');
}
```

### Will this tool help me reclaim `database` disk spaces?

[](#will-this-tool-help-me-reclaim-database-disk-spaces)

No, but if you are using this tool regularly to evict expired items, then you usually do not need to worry about reclaiming those disk spaces. For more details, talk with a system admin/database specialist.

### Can I run this tool during high-traffic hours?

[](#can-i-run-this-tool-during-high-traffic-hours)

Sure! This tool is designed with high-traffic situations in mind. It is memory-efficient and (for `database` caches) non-blocking. Feel free to run this tool e.g. hourly.

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance88

Actively maintained with recent releases

Popularity39

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 99.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~36 days

Recently: every ~41 days

Total

13

Last Release

59d ago

Major Versions

1.0.3 → 2.0.02025-01-12

PHP version history (2 changes)1.0.3PHP ^8.1

2.1.0PHP ^8.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/c11ba39366ac06b348619a7db14816075a374a8dd6895cbb7016892507dab5bc?d=identicon)[Vectorial1024](/maintainers/Vectorial1024)

---

Top Contributors

[![Vectorial1024](https://avatars.githubusercontent.com/u/17726797?v=4)](https://github.com/Vectorial1024 "Vectorial1024 (234 commits)")[![rubybrowncoat](https://avatars.githubusercontent.com/u/973824?v=4)](https://github.com/rubybrowncoat "rubybrowncoat (1 commits)")

---

Tags

cachelaravelphplaraveldatabasecachefilecleandeleteremoveclearexpiredgcevict

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/vectorial1024-laravel-cache-evict/health.svg)

```
[![Health](https://phpackages.com/badges/vectorial1024-laravel-cache-evict/health.svg)](https://phpackages.com/packages/vectorial1024-laravel-cache-evict)
```

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[illuminatech/config

Provides support for Laravel application runtime configuration managed in persistent storage

14921.0k1](/packages/illuminatech-config)[ytake/laravel-couchbase

Couchbase providers for Laravel

3051.9k](/packages/ytake-laravel-couchbase)[arifhp86/laravel-clear-expired-cache-file

Remove laravel expired cache file/folder

36128.7k](/packages/arifhp86-laravel-clear-expired-cache-file)[eusonlito/laravel-database-cache

Cache Database Query results on Laravel Query Builder or Eloquent

194.2k](/packages/eusonlito-laravel-database-cache)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
