Featured blog image
324 words 2 min read

How to Handle Failed Jobs in Laravel Queue (Step-by-Step)

Table Of Contents

When using Laravel Queue to run background tasks, you may face job failures due to connection issues or code errors. In this article, we’ll explain how Laravel handles failed jobs, how to control retry attempts, and how to manage errors step by step.

What Does a Failed Job Mean?

A job fails when an exception (Exception) occurs during execution. Laravel records that failure and decides whether to retry based on your configuration.

Number of Attempts

You can define how many times Laravel should retry a job before marking it as permanently failed.

public $tries = 3;

The default is 1, but you can increase it (for example, 3) to allow multiple attempts.

Setting a Maximum Timeout

If a job takes too long to complete, you can set a maximum execution time:

public $timeout = 30; // in seconds

Handling Final Failure

When a job fails after all retry attempts, Laravel automatically calls a method named failed() inside the job class. You can use it to log the error, send a notification, or perform any necessary action.

public function failed(Exception $exception)
{
    Log::error('Job failed: ' . $exception->getMessage());
}

Enable the failed_jobs Table

Laravel stores permanently failed jobs in a table named failed_jobs. To enable it, run the following commands:

php artisan queue:failed-table
php artisan migrate

You’ll then find a new table in your database that logs each failure — including the job name, error message, and failure timestamp.

Retry Failed Jobs

To retry all failed jobs:

php artisan queue:retry all

Or retry a specific job by its ID from the failed_jobs table:

php artisan queue:retry 5

Delete Failed Jobs

Once you’ve reviewed and fixed the issues, you can clear failed records:

php artisan queue:flush

This command deletes all records from the failed_jobs table.

Pro Tip

Always define a failed() method to log errors or send alerts — especially in production environments. It helps you catch issues early and keep your system stable.


Share Now ?
Let's chat