Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

How to clean up huge tables in Laravel with ease

Original – by Freek Van der Herten – 3 minute read

Using our laravel-model-cleanup package, you can easily remove old unneeded records from your database. We recently released a new major version that adds support for safely cleaning out huge tables.

In this blog post, I'd like to introduce this new release of the package to you.

Using the package

The models you wish to clean up should have a method cleanUp which returns the configuration of how the model should be cleaned up.

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

use Illuminate\Database\Eloquent\Model;
use Spatie\ModelCleanup\CleanupConfig;
use Spatie\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 five days ago.

It's that simple!

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

If you need more fine-grained control over when records are considered old, you can use the olderThan method. Here's an example where all models older than a year are considered old.

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

By default, models get cleaned up by performing a single delete query. When you want to clean up a huge 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 five days will be removed in chunks of 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 removed.

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

Watch me create this package

I've streamed most of the development process live on YouTube. These sessions were unrehearsed, and I've tried as much as possible to think aloud.

Session 1

Session 2

I also took the opportunity to use Pest for the test suite. In this stream, Pest creator Nuno Maduro joined to assist me in converting PHPUnit to Pest tests.

In closing

The package has some more handy features not mentioned in this blog post. Head over to the readme of the package on GitHub to know more. I'm already using this package in a couple of projects, and I hope it's helpful for you.

This isn't the only package that our team has created. Take a look at this big list of packages we created previously. I'm sure there will be something that could be of use in your next project.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

What are your thoughts on "How to clean up huge tables in Laravel with ease"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.