How to cancel queued job in Laravel or Redis
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Canceling Queued Jobs in Laravel and Redis
Introduction: In this blog post, we will discuss the process of canceling queued jobs in Laravel and Redis, covering various aspects such as identifying pending jobs from a specific Mailable, locating the job within your Redis queue, and ultimately, canceling it. We'll also explore some available tools, like FastoRedis, to make this task easier.
Browsing Pending Jobs in Laravel
To understand how we can locate and interact with pending jobs, you need to know that Laravel uses a queue system to handle time-consuming tasks asynchronously. It stores these queued jobs in various storage types, with Redis being one of the most popular options due to its speed and flexibility.Identifying Pending Jobs from a Specific Mailable
In your Laravel code, you can identify pending jobs by their queue name or job class. In your case, we assume that your Mailable is based on the 'MyCustomMailable' class. To find all the pending jobs related to this class, you might use the following code:use Illuminate\Support\Facades\Queue;
$jobs = Queue::getPendingRelease(null, function ($job) {
// check if job is an instance of MyCustomMailable
return $job instanceof \App\Jobs\MyCustomMailable;
});
This code will retrieve all pending jobs with the specified class and store them in the $jobs array. You can then iterate through these jobs to perform any necessary actions, such as canceling them.
Finding a Specific Pending Job via Redis
Now that we've identified pending jobs for our Mailable class, let's look at how you might use FastoRedis or other tools to browse the queue and locate the job you wish to cancel. To achieve this using FastoRedis: 1. Install FastoRedis by running 'composer require predis/predis'. 2. Create a new command line tool using Artisan and add it to your app's command list. 3. Inside the command, you can use the FastoRedis client to browse the Redis queue:use Illuminate\Support\Facades\Artisan;
use Predis\Client as Redis;
public function handle() {
$redis = new Redis();
$queueName = 'my_custom_mailable'; // update with your queue name
// list all pending jobs in the queue
foreach (iterator_to_array($redis->brpop(0, 100000, $queueName)) as $job) {
echo $job . PHP_EOL;
}
}
4. Run your command and review the output to find the job ID of the Mailable you wish to cancel.