Here at Honeybadger, we use Sidekiq a lot, and it's great. But there's one problem I've often run into.
...you see, I don't typically run Sidekiq when I'm in development. But in the course of manual testing, I cause lots of jobs to be enqueued. So the next time I do run Sidekiq it tries to process all of those jobs. Not good.
It's easy to find code snippets that will delete the jobs from one Sidekiq queue. But we have lots of queues. I want to clear the jobs from all of them. After a little digging, I came up with an answer that seems to work well. Behold!
# I originally had a more verbose piece of code here but mperham, Sidekiq's creator, set me straight :)
Sidekiq::Queue.all.each(&:clear)
There are more direct ways to go about it, but this one uses only methods defined in Sidekiq's public API, so hopefully it'll keep working even if the internals change.
Now with more emoji!
If you do a careful reading of the sidekiq source, you may notice you can use the 💣 emoji to invoke the clear method. No joke:
# https://github.com/mperham/sidekiq/blob/master/lib/sidekiq/api.rb#L255
alias_method :💣, :clear
So we can rewrite our "clear all queues" code like so:
Sidekiq::Stats.new.queues.each { |k, v| Sidekiq::Queue.new(k).💣 }
And since emoji's are so cool, we can make our own 💀 method to do the mass deletion.
def 💀
Sidekiq::Stats.new.queues.each { |queue_name, _| Sidekiq::Queue.new(queue_name).💣 }
end