diff --git a/app/Console/Commands/Admin/PruneOldNotifications.php b/app/Console/Commands/Admin/PruneOldNotifications.php new file mode 100644 index 000000000..3283479a7 --- /dev/null +++ b/app/Console/Commands/Admin/PruneOldNotifications.php @@ -0,0 +1,50 @@ +subMonths((int) $this->option('months')); + $batch = max(100, (int) $this->option('batch')); + + $this->info("Pruning notifications older than {$cutoff->toDateTimeString()}"); + $this->info("Batch size: {$batch}"); + + $total = 0; + + do { + $deleted = DB::delete( + ' + DELETE FROM `notifications` + WHERE `created_at` < ? + ORDER BY `created_at` + LIMIT ? + ', + [$cutoff, $batch] + ); + + $total += $deleted; + + $this->line("Deleted {$deleted} rows ({$total} total)"); + + if ($deleted > 0) { + usleep(100_000); + } + } while ($deleted === $batch); + + $this->info("Finished. Deleted {$total} notifications."); + + return self::SUCCESS; + } +}