How to clear all Sidekiq queues, using the power of emoji

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.

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
What to do next:
  1. Try Honeybadger for FREE
    Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
    Start free trial
    Easy 5-minute setup — No credit card required
  2. Get the Honeybadger newsletter
    Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo

    Starr Horne

    Starr Horne is a Rubyist and Chief JavaScripter at Honeybadger.io. When she's not neck-deep in other people's bugs, she enjoys making furniture with traditional hand-tools, reading history and brewing beer in her garage in Seattle.

    More articles by Starr Horne