PHPackages                             codenco-dev/laravel-eloquent-pruning - 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. codenco-dev/laravel-eloquent-pruning

AbandonedArchivedLibrary

codenco-dev/laravel-eloquent-pruning
====================================

We often have to clean up our oldest or unnecessary records. This package does it easily for us.

v1.2.0(5y ago)1522MITPHPPHP ^7.2.5

Since Jun 9Pushed 5y ago1 watchersCompare

[ Source](https://github.com/codenco-dev/laravel-eloquent-pruning)[ Packagist](https://packagist.org/packages/codenco-dev/laravel-eloquent-pruning)[ Docs](https://github.com/codenco-dev/laravel-eloquent-pruning)[ GitHub Sponsors](https://github.com/thomasdominic)[ RSS](/packages/codenco-dev-laravel-eloquent-pruning/feed)WikiDiscussions master Synced today

READMEChangelog (5)Dependencies (6)Versions (6)Used By (0)

Easily prune your Eloquent Model Records
========================================

[](#easily-prune-your-eloquent-model-records)

[![Latest Version on Packagist](https://camo.githubusercontent.com/eba12c7d8d9e66fa12232d5b43bc27353600b446cdaedf5fb980f1371dd83328/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f64656e636f2d6465762f6c61726176656c2d656c6f7175656e742d7072756e696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/codenco-dev/laravel-eloquent-pruning)[![GitHub Tests Action Status](https://camo.githubusercontent.com/318c9bbf063c7c0769026e36daf45b50792623ff4ca1500ff4d0b4c1482e4eaa/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f636f64656e636f2d6465762f6c61726176656c2d656c6f7175656e742d7072756e696e672f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/codenco-dev/laravel-eloquent-pruning/actions?query=workflow%3Arun-tests+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/70728b132174bca0ff466cfeeeff74cd58cde278b96b3112e75f6aee33c1dbe1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f64656e636f2d6465762f6c61726176656c2d656c6f7175656e742d7072756e696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/codenco-dev/laravel-eloquent-pruning)

We often have to clean up the records of our databases by deleting the oldest and useless data. This package allows, via Eloquent models, to make this easier.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require codenco-dev/laravel-eloquent-pruning
```

Configuration
-------------

[](#configuration)

We have several options that can be configure.

- `pruning_column` - The name of the column that will be used to determine if a record should be pruned
- `hours` - The hours count that will determine if a record should pruned relative to pruning\_column and now datetime
- `with_delete_events` - If the value is true, the delete method of model will be call, allowing fire events. If the value is false, the delete action will be done with query builder, without event.
- `chunk_size` - The size of delete query if with\_delete\_events is false

If all prunable models have the same configuration, you can do it in the `laravel-eloquent-pruning` file. You can publish configuration file with this command line

```
php artisan vendor:publish --provider="CodencoDev\LaravelEloquentPruning\LaravelEloquentPruningServiceProvider" --tag="config"
```

If all prunable models don't have the same configuration, each options for a model can be defined in the model file like this :

```
    class MyModel extends Model
    {
        use Prunable;

        protected $fillable = ['id'];

        protected $hours = 1;
        protected $pruningColumn = 'updated_at';
        protected $chunkSize = 1000;
        protected $withDeleteEvents = false;

    }
```

If you need something more elaborate, you be able to overwrite, `Prunable` trait methods.

```
    class MyModel extends Model
    {
        use Prunable;

        protected $fillable = ['id'];

        public function getHours(): int
        {
            return 1;
        }

        public function getPruningColumn(): string
        {
            return 'created_at';
        }

        public function getChunkSize(): int
        {
            return 1000;
        }

        public function getWithDeleteEvents(): bool
        {
            return false;
        }
    }
```

In configuration file, you can manage default value for the Pruning Package. You must define models that will be affected by pruning.

```
    'models' => [
        App\MyModel::class,
        App\MySecondModel::class,
    ],
```

If some records can't be prune, with business logic, you can use this methods :

If `with_delete_events` is false, the business logic must be put in `scopeCouldBePruned` like this

```
    /**
     * Scope that allows filter records for pruning.
     */
    public function scopeCouldBePruned(Builder $query): Builder
    {
        return $query->where('status','done');
    }
```

If `with_delete_events` is true, the business logic can be put in `scopeCouldBePruned` but also by overwrite `canBePruned` method like this

```
    /**
     * Define if the active record can be pruned, if the ProcessWithDeleteEvents is true.
     */
    public function canBePruned(): bool
    {
        //tests, extern call etc.
        return MyService::checkMyModelIsDeletable($this->id);
    }
```

Usage
-----

[](#usage)

You can add global pruning on your schedule by modifying `app/Console/Kernel.php` like this for example

```
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('pruning:start')->hourly();
    }
```

If you want bypass model hour configuration, you can call this command with `hours` option like this :

```
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('pruning:start --hours=48])->hourly();
    }
```

This call allows to prune all data created more than 48 hours ago.

Of course, you can use a schedule by model (or create a dedicated command if you want) :

```
    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function(){
            (new MyModel)->prune();
        })->daily();
    }
```

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Dominic Thomas](https://github.com/codenco-dev)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Laravel Package Boilerplate
---------------------------

[](#laravel-package-boilerplate)

This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).

Inspiration
-----------

[](#inspiration)

This package was inspired by laravel/telescope pruning.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~43 days

Total

5

Last Release

1989d ago

Major Versions

0.1.0 → 1.0.02020-06-11

PHP version history (2 changes)0.1.0PHP ^7.4

1.0.0PHP ^7.2.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/19ce7305c373d4ab9f6633a2d2ddb3693ae27a1fdd2897244a6e3e4c9760d581?d=identicon)[DomThomas-Dev](/maintainers/DomThomas-Dev)

---

Top Contributors

[![domthomas-dev](https://avatars.githubusercontent.com/u/17202290?v=4)](https://github.com/domthomas-dev "domthomas-dev (50 commits)")

---

Tags

codenco-devlaravel-eloquent-pruning

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/codenco-dev-laravel-eloquent-pruning/health.svg)

```
[![Health](https://phpackages.com/badges/codenco-dev-laravel-eloquent-pruning/health.svg)](https://phpackages.com/packages/codenco-dev-laravel-eloquent-pruning)
```

###  Alternatives

[fumeapp/modeltyper

Generate TypeScript interfaces from Laravel Models

196277.9k](/packages/fumeapp-modeltyper)

PHPackages © 2026

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