Get "PHP 8 in a Nuthshell" (Soon includes PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Artisan command to prune stale jobs in Laravel 8.x

In Laravel, when your queued jobs fail after a job has exceeded the number of attempts, it will be inserted into the failed_jobs database table (if you have set it up).

This is to provision for retrying those jobs at a later time using the following command.

php artisan queue:retry 5

Here 5 is the ID of the failed job that you want to retry or you can also provide the range of IDs as well using --range options like so.

$ php artisan queue:retry --range=5-10

Now, there might be situations where there would be a lot of failed jobs in the database and some of which are too old that they are not relevant anymore. It’s best to remove those entries and that’s where this new Artisan command in Laravel 8.x comes into the picture.

The queue:prune-failed command

Mohamed Said has implemented this Artisan command called queue:prune-failed in the recent minor release using which you can remove stale failed job entries from the failed_jobs table.

$ php artisan queue:prune-failed

Now, by default, this command will remove the entries which are older than 24 hours but if you want you can also specify an --hours option with the number of hours to retain jobs data.

So, if you want to retain jobs data of only the last 5 hours and remove the rest of the entries, you can do it like so.

$ php artisan queue:prune-failed --hours=5
Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?