Skip to content

Instantly share code, notes, and snippets.

@lukaswhite
Last active March 20, 2023 08:11
Show Gist options
  • Save lukaswhite/8882024 to your computer and use it in GitHub Desktop.
Save lukaswhite/8882024 to your computer and use it in GitHub Desktop.
Clear a Beanstalkd Queue in Laravel
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class ClearBeanstalkdQueueCommand extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'queue:beanstalkd:clear';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Clear a Beanstalkd queue, by deleting all pending jobs.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Defines the arguments.
*
* @return array
*/
public function getArguments()
{
return array(
array('queue', InputArgument::OPTIONAL, 'The name of the queue to clear.'),
);
}
/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$queue = ($this->argument('queue')) ? $this->argument('queue') : Config::get('queue.connections.beanstalkd.queue');
$this->info(sprintf('Clearing queue: %s', $queue));
$pheanstalk = Queue::getPheanstalk();
$pheanstalk->useTube($queue);
$pheanstalk->watch($queue);
while ($job = $pheanstalk->reserve(0)) {
$pheanstalk->delete($job);
}
$this->info('...cleared.');
}
}
@nsbucky
Copy link

nsbucky commented Nov 10, 2014

thank you. this saved my life this morning.

@hafizbadrie
Copy link

This also saved my life, this afternoon. :)

@jfhernandeze
Copy link

I am getting this error when running php artisan queue:beanstalkd:clear

[InvalidArgumentException]
There are no commands defined in the "queue:beanstalkd" namespace.
Did you mean this?
queue

@nicekiwi
Copy link

@jfhernandeze You need to register the command in your app/start/artisan.php file:

Artisan::add(new ClearBeanstalkdQueueCommand);

@morrislaptop
Copy link

I've made a composer package for this to help anyone else out - https://github.com/morrislaptop/laravel-queue-clear

@colinc
Copy link

colinc commented Mar 4, 2015

This is awesome!

Just to be clear, the Gist above by @lukaswhite is for Laravel 4 and the package by @morrislaptop is for Laravel 5.

@cowboymathu
Copy link

Life saver :)

@hvent90
Copy link

hvent90 commented Jun 8, 2015

THANK YOUUUUUUU

@zach2825
Copy link

I don't know if this will help anyone. Delete a specific job from beanstalk for laravel 4.

$res = Queue::getPheanstalk();
$job = $res->peek(29); //29 is the job id i want to delete
$res->delete($job);

@cheelahim
Copy link

I've posted my implementation for Laravel 5.1 https://gist.github.com/cheelahim/ded1ab9e5e9a290fe9fc

@shohe05
Copy link

shohe05 commented Sep 1, 2015

Thanks so much!!

@christiaan-lombard
Copy link

Awesome, thanks!

@lokielse
Copy link

lokielse commented Mar 1, 2016

Awesome, thanks!

@Donny5300
Copy link

Great! 👍

@getafixx
Copy link

thanks

@2bj
Copy link

2bj commented Dec 8, 2017

@Igzak
Copy link

Igzak commented Jul 28, 2020

thanks 👍

@fbolehhrynkiv
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment