#database #development #eloquent #laravel #php

Sometimes you may want to periodically delete models that are no longer needed. To accomplish this, you may add the Illuminate\Database\Eloquent\Prunable or Illuminate\Database\Eloquent\MassPrunable trait to the models you would like to periodically prune. After adding one of the traits to the model, implement a prunable method which returns an Eloquent query builder that resolves the models that are no longer needed:

 1namespace App\Models;
 2
 3use Illuminate\Database\Eloquent\Model;
 4use Illuminate\Database\Eloquent\Prunable;
 5
 6class Flight extends Model
 7{
 8    use Prunable;
 9
10    /**
11     * Get the prunable model query.
12     *
13     * @return \Illuminate\Database\Eloquent\Builder
14     */
15    public function prunable()
16    {
17        return static::where('created_at', '<=', now()->subMonth());
18    }
19}

When marking models as Prunable, you may also define a pruning method on the model. This method will be called before the model is deleted. This method can be useful for deleting any additional resources associated with the model, such as stored files, before the model is permanently removed from the database:

1/**
2 * Prepare the model for pruning.
3 *
4 * @return void
5 */
6protected function pruning()
7{
8    //
9}

After configuring your prunable model, you should schedule the model:prune Artisan command in your application's App\Console\Kernel class. You are free to choose the appropriate interval at which this command should be run:

 1/**
 2 * Define the application's command schedule.
 3 *
 4 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 5 * @return void
 6 */
 7protected function schedule(Schedule $schedule)
 8{
 9    $schedule->command('model:prune')->daily();
10}

Behind the scenes, the model:prune command will automatically detect "Prunable" models within your application's app/Models directory. If your models are in a different location, you may use the --model option to specify the model class names:

1$schedule->command('model:prune', [
2    '--model' => [Address::class, Flight::class],
3])->daily();

Notice: Soft deleting models will be permanently deleted (forceDelete) if they match the prunable query.

Mass Pruning

When models are marked with the Illuminate\Database\Eloquent\MassPrunable trait, models are deleted from the database using mass-deletion queries. Therefore, the pruning method will not be invoked, nor will the deleting and deleted model events be dispatched. This is because the models are never actually retrieved before deletion, thus making the pruning process much more efficient:

 1namespace App\Models;
 2
 3use Illuminate\Database\Eloquent\Model;
 4use Illuminate\Database\Eloquent\MassPrunable;
 5
 6class Flight extends Model
 7{
 8    use MassPrunable;
 9
10    /**
11     * Get the prunable model query.
12     *
13     * @return \Illuminate\Database\Eloquent\Builder
14     */
15    public function prunable()
16    {
17        return static::where('created_at', '<=', now()->subMonth());
18    }
19}