diff --git a/src/Illuminate/Bus/DatabaseBatchRepository.php b/src/Illuminate/Bus/DatabaseBatchRepository.php index 993f6f49b643..624da19ae6e0 100644 --- a/src/Illuminate/Bus/DatabaseBatchRepository.php +++ b/src/Illuminate/Bus/DatabaseBatchRepository.php @@ -277,6 +277,29 @@ public function pruneUnfinished(DateTimeInterface $before) return $totalDeleted; } + /** + * Prune all of the cancelled entries older than the given date. + * + * @param \DateTimeInterface $before + * @return int + */ + public function pruneCancelled(DateTimeInterface $before) + { + $query = $this->connection->table($this->table) + ->whereNotNull('cancelled_at') + ->where('created_at', '<', $before->getTimestamp()); + + $totalDeleted = 0; + + do { + $deleted = $query->take(1000)->delete(); + + $totalDeleted += $deleted; + } while ($deleted !== 0); + + return $totalDeleted; + } + /** * Execute the given Closure within a storage specific transaction. * diff --git a/src/Illuminate/Queue/Console/PruneBatchesCommand.php b/src/Illuminate/Queue/Console/PruneBatchesCommand.php index e8a292d34aca..5e2d32337055 100644 --- a/src/Illuminate/Queue/Console/PruneBatchesCommand.php +++ b/src/Illuminate/Queue/Console/PruneBatchesCommand.php @@ -19,7 +19,8 @@ class PruneBatchesCommand extends Command */ protected $signature = 'queue:prune-batches {--hours=24 : The number of hours to retain batch data} - {--unfinished= : The number of hours to retain unfinished batch data }'; + {--unfinished= : The number of hours to retain unfinished batch data } + {--cancelled= : The number of hours to retain cancelled batch data }'; /** * The name of the console command. @@ -65,5 +66,15 @@ public function handle() $this->components->info("{$count} unfinished entries deleted."); } + + if ($this->option('cancelled')) { + $count = 0; + + if ($repository instanceof DatabaseBatchRepository) { + $count = $repository->pruneCancelled(Carbon::now()->subHours($this->option('cancelled'))); + } + + $this->components->info("{$count} cancelled entries deleted."); + } } }