Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AJEllender's avatar

Remove Jobs from Database queue

I'm looking to set up a system that sends out an email on a given day. Queued jobs seemed like the correct solution to this, using the delay. However, the email author should retain the ability to change when the email is sent out. I store the job ID, but after queueing a job to the database driver, there doesn't seem to be any way to get that job back out of the queue and delete it before it runs.

I 'could' manually do it with something like:

app('queue')->getDatabase()->table('jobs')->where('id', $job_id)->delete();

But it feels pretty dirty, and as if there should be a better way. IS there a better way to do it? Alternatively, am I just approaching this all wrong and there's a much better/ simpler way to trigger a job at a given time while still being able to cancel it any time beforehand?

0 likes
7 replies
daiv's avatar

Did you find a solution?

1 like
AJEllender's avatar

Sadly, no. I spent another couple of hours digging around and came to conclusion that I had something that I knew would at least work (and has continued to do so), so I called it a day and left it as it was.

A cleaner way might be to keep a record somewhere in your app of job ids that have been (or should be) cancelled, and then hook intoQueue::before to check and remove jobs that don't need running... but it still doesn't seem as good as I'd like.

1 like
jyk2000's avatar

Whoever having same issue, you can unserialize the payload in the jobs table record into the job class you dispatched before.

$jobs = DB::table('jobs')->get();

foreach ($jobs as $job) {

$payload = json_decode($job->payload);

$obj = unserialize($payload->data->command);  

### this is job class object unserialized. 

}

$job->id returns jobs table ID

$obj should hold all public properties you need to investigate to find the item you want to remove.

now use the $job->id to delete record in the jobs table

Hope this helps.

stalinko's avatar

@jyk2000 your solution is too greedy. There can be thousands or millions of jobs. That's very inefficient to analyse all the jobs on regular basis.

Please or to participate in this conversation.