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

diblasid's avatar

Queue:clear doesn't clear unique locks too?

Dear all, I'm studying queues and processes, When I want a process to be unique I set its' class as:

class MyProcess implements ShouldQueue, ShouldBeUnique{
public $uniqueFor = 3600;
public function uniqueId(): string
    {
        return ... < UNIQUE ID >
    }

I can see that when I fire a MyProcess ::dispatch(); it is added to the "jobs" table AND a file under the <storageFolder>/cache/data/... is created (the lock file)

when I try to fire again: MyProcess ::dispatch(); nothing happens, because of the ShouldBeUnique, well, it works.

Then let say, for any reason, I may have to fire the command: artisan queue:clear to unqueue the process from the queue. I've seen that the clear command doesn't remove the lock file under <storageFolder>/cache/data/... As a result when I fire again MyProcess ::dispatch(), it doesnt get queued in the jobs table.

Which is the proper way to unqueue a job whith ShouldBeUnique feature?

Many thanks P.S. I've not found the proper forum section where to put this thread into, feel free to move it or suggest me the right one.

1 like
2 replies
ramonrietdijk's avatar

The ShouldBeUnique interface will be used by Laravel to determine if the job should be unique and saves the unique id. This always uses a cache repository and therefore is not cleared after queue:clear.

If a job does not need to run anymore given a certain condition, it will be the easiest to return early. An example for this would be to stop executing a job from a cancelled batch.

public function handle(): void
{
    if ($cancel) {
        return;
    }

    // ...
}

Laravel will automatically remove the unique cache key of the job if it has finished executing. Note that if you kill the process before the job is finished, with a dd for example, you will have to clear the cache yourself.

2 likes

Please or to participate in this conversation.