140

I've this worker that runs for ever.

class Worker
  include Sidekiq::Worker
  sidekiq_options queue: "infinity", retry: true

  def perform(params)
    # ...
    self.class.perform_in(30.seconds, params)
  end
end

The problem is that I load workers on start up, like this. config/initializers/load_workers.rb

Rails.application.config.after_initialize do  
  if ENV["SIDEKIQ"] == "1"
    Worker.perform_async({})
  end
end

Using this to start sidekiq SIDEKIQ=1 sidekiq --verbose --environment production -C config/sidekiq.yml.

This means that old workers as to stop, both those currently running but also the ones being rescheduled.

I tried running this on start up (just before loading new works), but that didn't work.

q = []
q += Sidekiq::RetrySet.new.select { |job| job.klass.match(/Worker/) }
q += Sidekiq::Queue.new("infinity").select { |job| job.klass.match(/Worker/) }
q += Sidekiq::ScheduledSet.new.select { |job| job.klass.match(/Worker/) }
q.each(&:delete)

After 5-ish deploys there are bunch of duplicate workers in the queue scheduled for later. So, is there a way to clear everyting in one queue and prevent already running jobs from rescheduling?

I'm using sidekiq 3.0.

5
  • Do you want to delete a queue or a scheduled job. or better to say duplicate scheduled job. Apr 18, 2014 at 6:27
  • I want to delete every trace of a job before I load the new worker. Apr 18, 2014 at 10:11
  • ok got your problem .. means you want to delete all the scheduled jobs. Apr 18, 2014 at 10:25
  • @haSabyasachiGhosh No, I want to delete everyting related to a queue, not just the scheduled jobs. In other words; failed, ongoing, queued and scheduled jobs should be deleted. Apr 18, 2014 at 11:30
  • All help is here - github.com/mperham/sidekiq/wiki/API
    – viks
    Jun 9, 2020 at 9:16

5 Answers 5

203

Deletes all Jobs in a Queue, by removing the queue.

require 'sidekiq/api' # for the case of rails console

Sidekiq::Queue.new("infinity").clear
Sidekiq::RetrySet.new.clear
Sidekiq::ScheduledSet.new.clear
6
  • Might be Sidekiq Pro will help you. May 15, 2014 at 10:49
  • 6
    Here is to clear the counters Sidekiq::Stats.new.reset I find out that this helps out
    – user4203675
    Dec 3, 2014 at 15:46
  • 16
    To clear the default/first queue: Sidekiq::Queue.all.first.clear
    – user456584
    Mar 23, 2015 at 21:39
  • 2
    The Sidekiq::Queue.new("reports").clear and Sidekiq::Queue.new("alerts").clear helps clear the current queue. But what about Busy? How do I clear the "Busy" jobs?
    – Donato
    May 25, 2015 at 6:19
  • 40
    Clear them all with Sidekiq::Queue.all.each &:clear Dec 10, 2015 at 23:09
184

This will clear all queues, schedules and retries:

require 'sidekiq/api'
Sidekiq::Queue.all.each(&:clear)
Sidekiq::RetrySet.new.clear
Sidekiq::ScheduledSet.new.clear
Sidekiq::DeadSet.new.clear
2
  • 1
    Thanks this worked like a charm i was into an emergency haha , the sidekick was with waaaaaay too many workers on the queue this worked as i was expecting since i was looking for kill like command
    – d1jhoni1b
    Mar 30, 2018 at 17:03
  • 1
    Perfect, just adding that in my env I had to require 'sidekiq/api' on console to work. Jan 2, 2020 at 16:50
21

Works for me for most sidekiq versions:

Sidekiq::RetrySet.new.clear

Sidekiq::ScheduledSet.new.clear

Clear statistics (Optional)

Sidekiq::Stats.new.reset
2
  • This flushes the entire database, not just the queue. Jul 21, 2017 at 7:06
  • if will flush all the database of sidekiq, and will work fine in rails console
    – vidur punj
    Apr 20, 2020 at 8:27
3

There is one more convenient way to clear all Sidekiq queues and sets for local env: Sidekiq.redis(&:flushdb)

Basically, it just flushes Redis that is configured for Sidekiq, so I would avoid using it in a non-local environment, as it can also remove some data you store in Redis. Anyways, it can be useful for the development or when you have a separate Redis instance for Sidekiq. And it's just one line instead of four.

-4

You can clear your queue by running this code although there would be built-in methods.

queue = Sidekiq::Queue.new
queue.each do |job|
  job.delete 
end
3
  • Well, this should work as we fetched all the jobs from queue and then deleted them one by one. Oct 2, 2018 at 6:55
  • No it doesn’t. Take a look at the existing answers. It’s also a duplicate of the existing answer. Oct 2, 2018 at 6:57
  • I don't know what are you talking about. Can you please show me the same answer i posted? Oct 2, 2018 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.