Skip to content

Commit

Permalink
Add 'mode' option to purge command to support soft deletions
Browse files Browse the repository at this point in the history
  • Loading branch information
alajusticia committed Jul 18, 2023
1 parent 444fdb0 commit 5b4b98b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,14 @@ You can also specify a period of time to delete models expired since that given
php artisan expirable:purge "App\Models\Subscription" --since="2 months"
```

By default, this command will use force deletion to purge the models. If needed, you can specify the `mode` option with the value `soft` to perform a soft delete:

```
php artisan expirable:purge --mode=soft
```

:warning: Be aware that if you try to purge models that have foreign key constraints using the default `hard` mode, you will have to specify the desired action for the "on delete" property of the constraint (for example using the onDelete('cascade'), cascadeOnDelete() or nullOnDelete() modifiers on the foreign key in your migrations: [https://laravel.com/docs/9.x/migrations#foreign-key-constraints](https://laravel.com/docs/9.x/migrations#foreign-key-constraints)) or purge the child models first (ordering the command arguments or the array in the config file to start with the children) to avoid SQL errors.

## License

Open source, licensed under the [MIT license](LICENSE).
10 changes: 10 additions & 0 deletions config/expirable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@

'attribute_name' => 'expires_at',

/*
|--------------------------------------------------------------------------
| Mode
|--------------------------------------------------------------------------
|
| Whether the expirable:purge command deletion defaults to hard or soft.
| Defaults to hard for backward compatibility.
|
*/
'mode' => 'hard',

/*
|--------------------------------------------------------------------------
Expand Down
6 changes: 4 additions & 2 deletions src/Commands/PurgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class PurgeCommand extends Command
*/
protected $signature = 'expirable:purge
{model?* : Optional list of models to purge. If not provided, will take the models in the purge array of the configuration file.}
{--since= : Time since expiration.}';
{--since= : Time since expiration.}
{--mode= : Whether the deletion mode is "soft" (hard otherwise). If not provided, will take the mode value of the configuration file or default to "hard".}';

/**
* The console command description.
Expand All @@ -41,6 +42,7 @@ public function handle()
if (count($models)) {

$expiredSince = $this->option('since');
$mode = $this->option('mode') ?: Config::get('expirable.mode', 'hard');

$this->line('');
$this->comment('Deleting expired records...');
Expand All @@ -58,7 +60,7 @@ public function handle()
$query = call_user_func($purgeable . '::expiredSince', $expiredSince);
}

$total = $query->forceDelete();
$total = Str::lower($mode) == 'soft' ? $query->delete() : $query->forceDelete();

if ($total > 0) {
$this->info($total . ' ' . Str::plural('record', $total) . ' deleted.');
Expand Down

0 comments on commit 5b4b98b

Please sign in to comment.