PHPackages                             halo123450/laravel-model-cleanup - 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. halo123450/laravel-model-cleanup

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

halo123450/laravel-model-cleanup
================================

This package deletes unneeded records in a database.

v1.0.2(1y ago)043↓100%MITPHPPHP &gt;=8.0

Since Dec 16Pushed 1y agoCompare

[ Source](https://github.com/halo123450/laravel-model-cleanup)[ Packagist](https://packagist.org/packages/halo123450/laravel-model-cleanup)[ Docs](https://github.com/halo123450/laravel-model-cleanup)[ GitHub Sponsors](https://github.com/halo123450)[ RSS](/packages/halo123450-laravel-model-cleanup/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

**🚨 THIS PACKAGE IS NO LONGER MAINTAINED, WE RECOMMEND TO USE LARAVEL'S BUILT IN PRUNABLE 🚨**

Clean up unneeded records
=========================

[](#clean-up-unneeded-records)

[![Latest Version on Packagist](https://camo.githubusercontent.com/67b4262769bf4fd2435377970f2f1d7783004b5a44ccd0fe338f1f6192a259f5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68616c6f3132333435302f6c61726176656c2d6d6f64656c2d636c65616e75702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/halo123450/laravel-model-cleanup)[![Tests](https://github.com/halo123450/laravel-model-cleanup/workflows/run-tests/badge.svg)](https://github.com/halo123450/laravel-model-cleanup/workflows/run-tests/badge.svg)[![Psalm](https://github.com/halo123450/laravel-model-cleanup/workflows/Psalm/badge.svg)](https://github.com/halo123450/laravel-model-cleanup/workflows/Psalm/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/ba3e26631deec081a03658fd00f6ba079de0422561521585db0514355605702b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68616c6f3132333435302f6c61726176656c2d6d6f64656c2d636c65616e75702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/halo123450/laravel-model-cleanup)[![MIT Licensed](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

This package will clean up old records.

The models you wish to clean up should have a method `cleanUp` which returns the configuration how the model should be cleaned up. Here's an example where all records older than 5 days will be cleaned up.

```
use Illuminate\Database\Eloquent\Model;
use Halo123450\ModelCleanup\CleanupConfig;
use Halo123450\ModelCleanup\GetsCleanedUp;

class YourModel extends Model implements GetsCleanedUp
{
    ...

     public function cleanUp(CleanupConfig $config): void
     {
         $config->olderThanDays(5);
     }
}
```

After registering the model in the config file, running the `clean:models` artisan command will delete all records that have been created more than 5 days ago.

The package contains various other methods for specifying which records should be deleted.

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

[](#installation)

You can install the package via composer:

```
composer require halo123450/laravel-model-cleanup
```

Next, you must publish the config file:

```
php artisan vendor:publish --provider="Halo123450\ModelCleanup\ModelCleanupServiceProvider"
```

This is the content of the published config file `model-cleanup.php`.

```
return [

    /*
     * All models in this array that implement `Halo123450\ModelCleanup\GetsCleanedUp`
     * will be cleaned.
     */
    'models' => [
        // App\Models\YourModel::class,
    ],
];
```

Optionally, you can schedule the `Halo123450\ModelCleanup\Commands\CleanUpModelsCommand` to run at a frequency of which you want to clean up models. Here's an example where all models will be cleaned up every day at midnight.

```
// in app/Console/Kernel.php

protected function schedule(Schedule $schedule)
{
    $schedule->command(\Halo123450\ModelCleanup\Commands\CleanUpModelsCommand::class)->daily();
}
```

Usage
-----

[](#usage)

All models that you want to clean up must implement the `GetsCleanedUp`-interface. In the required `cleanUp`-method you can specify which records are considered old and should be deleted.

Here's an example where all records older than 5 days will be cleaned up.

```
use Illuminate\Database\Eloquent\Model;
use Halo123450\ModelCleanup\CleanupConfig;
use Halo123450\ModelCleanup\GetsCleanedUp;

class YourModel extends Model implements GetsCleanedUp
{
    ...

     public function cleanUp(CleanupConfig $config): void
     {
        $config->olderThanDays(5);
     }
}
```

Next, you should register this model in the `models` key of the `model-cleanup` config file.

```
// in config/model-cleanup.php

return [
    'models' => [
        App\Models\YourModel::class,
    ],

    // ...
]
```

When running the console command `clean:models` all models older than 5 days will be deleted.

### Soft deleted models

[](#soft-deleted-models)

This package also supports cleaning up models that have soft deleting enabled. Models that use the `Illuminate\Database\Eloquent\SoftDeletes` trait and are considered old, will be permanently removed from your database instead of being marked as deleted.

### Available methods on `CleanupConfig`

[](#available-methods-on-cleanupconfig)

### `olderThanDays`

[](#olderthandays)

Using this method you can mark records that have a `created_at` value older than a given number of days as old.

Here's an example where all models older than 5 days are considered old.

```
 public function cleanUp(CleanupConfig $config): void
 {
    $config->olderThanDays(5);
 }
```

### `olderThan`

[](#olderthan)

The `olderThan` method accepts an instance of `Carbon`. All models with a `created_at` value before that instance, will be considered old.

Here's an example where all models older than a year are considered old.

```
 public function cleanUp(CleanupConfig $config): void
 {
    $config->olderThan(now()->subYear());
 }
```

### `useDateAttribute`

[](#usedateattribute)

When using `olderThanDays` and `olderThan` methods, the deletion query that is built up behind the scenes will use the `created_at` column. You can specify an alternative column, using the `useDateAttribute` method.

```
 public function cleanUp(CleanupConfig $config): void
 {
    $config
        ->olderThanDays(5)
        ->useDateAttribute('custom_date_column');
 }
```

### `scope`

[](#scope)

Using the `scope` method you can make the query that will delete old records more specific.

Assume that your model has a `status` attribute. Only records with a status `inactive` may be cleaned up. Here's an example where all records with an `inactive` status that are older than 5 days will be cleaned up.

```
 public function cleanUp(CleanupConfig $config): void
 {
    $config
       ->olderThanDays(5)
       ->scope(fn (Illuminate\Database\Eloquent\Builder $query) => $query->where('status', 'inactive'));
}
```

### `chunk`

[](#chunk)

By default, models get cleaned up by performing a single `delete` query. When you want to clean up a very large table, this single query could lock your table for a long time. It even might not be possible to get the lock in the first place.

To solve this, the package can delete records in chunks using the `chunk` method.

In this example, all records older than 5 days will be deleted in chucks of a 1000 records.

```
 public function cleanUp(CleanupConfig $config): void
 {
    $config
       ->olderThanDays(5)
       ->chunk(1000);
}
```

The package will stop deleting records when there are no more left that should be deleted.

If you need more fine-grained control over when to stop deleting, you can pass a closure as a second argument to `chunk`. Returning `false` in the closure will stop the deletion process.

In the example below, the deletion process will continue until all records older than 5 days are deleted or the record count of the model goes below 5000.

```
 public function cleanUp(CleanupConfig $config): void
 {
    $config
       ->olderThanDays(5)
       ->chunk(1000, fn() => YourModel::count() > 5000);
}
```

Events
------

[](#events)

After the model has been cleaned `Halo123450\ModelCleanup\Events\ModelCleanedUp` will be fired even if there were no records deleted.

It has two public properties: `model`, which contains an instance of the model which was cleaned up. and `numberOfDeletedRecords`.

Changelog
---------

[](#changelog)

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

Testing
-------

[](#testing)

```
composer test
```

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

[](#contributing)

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

Security
--------

[](#security)

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

Postcardware
------------

[](#postcardware)

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Halo123450, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards [on our company website](https://s-3.cn/en/opensource/postcards).

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance40

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 67.4% 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 ~0 days

Total

2

Last Release

512d ago

### Community

Maintainers

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

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (91 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (9 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (4 commits)")[![jochensengier](https://avatars.githubusercontent.com/u/10118729?v=4)](https://github.com/jochensengier "jochensengier (3 commits)")[![brendt](https://avatars.githubusercontent.com/u/6905297?v=4)](https://github.com/brendt "brendt (3 commits)")[![halo123450](https://avatars.githubusercontent.com/u/45340765?v=4)](https://github.com/halo123450 "halo123450 (3 commits)")[![riasvdv](https://avatars.githubusercontent.com/u/3626559?v=4)](https://github.com/riasvdv "riasvdv (3 commits)")[![dmason30](https://avatars.githubusercontent.com/u/20278756?v=4)](https://github.com/dmason30 "dmason30 (1 commits)")[![drfraker](https://avatars.githubusercontent.com/u/1279323?v=4)](https://github.com/drfraker "drfraker (1 commits)")[![dwightwatson](https://avatars.githubusercontent.com/u/1100408?v=4)](https://github.com/dwightwatson "dwightwatson (1 commits)")[![francislavoie](https://avatars.githubusercontent.com/u/2111701?v=4)](https://github.com/francislavoie "francislavoie (1 commits)")[![bolechen](https://avatars.githubusercontent.com/u/195015?v=4)](https://github.com/bolechen "bolechen (1 commits)")[![wardhache](https://avatars.githubusercontent.com/u/23256045?v=4)](https://github.com/wardhache "wardhache (1 commits)")[![BackEndTea](https://avatars.githubusercontent.com/u/14289961?v=4)](https://github.com/BackEndTea "BackEndTea (1 commits)")[![lloy0076](https://avatars.githubusercontent.com/u/1174532?v=4)](https://github.com/lloy0076 "lloy0076 (1 commits)")[![michelecurletta](https://avatars.githubusercontent.com/u/65455871?v=4)](https://github.com/michelecurletta "michelecurletta (1 commits)")[![nunomaduro](https://avatars.githubusercontent.com/u/5457236?v=4)](https://github.com/nunomaduro "nunomaduro (1 commits)")[![Omranic](https://avatars.githubusercontent.com/u/406705?v=4)](https://github.com/Omranic "Omranic (1 commits)")[![pktharindu](https://avatars.githubusercontent.com/u/23132672?v=4)](https://github.com/pktharindu "pktharindu (1 commits)")[![willemwollebrants](https://avatars.githubusercontent.com/u/916958?v=4)](https://github.com/willemwollebrants "willemwollebrants (1 commits)")

---

Tags

laravel-model-cleanuphalo123450

### Embed Badge

![Health badge](/badges/halo123450-laravel-model-cleanup/health.svg)

```
[![Health](https://phpackages.com/badges/halo123450-laravel-model-cleanup/health.svg)](https://phpackages.com/packages/halo123450-laravel-model-cleanup)
```

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135192.6k5](/packages/statamic-rad-pack-runway)[highsolutions/eloquent-sequence

A Laravel package for easy creation and management sequence support for Eloquent models with elastic configuration.

121130.3k](/packages/highsolutions-eloquent-sequence)[dragon-code/migrate-db

Easy data transfer from one database to another

15717.4k](/packages/dragon-code-migrate-db)

PHPackages © 2026

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