Clear sidekiq queue

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.


Solution 1:

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

Solution 2:

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

Solution 3:

Works for me for most sidekiq versions:

Sidekiq::RetrySet.new.clear

Sidekiq::ScheduledSet.new.clear

Clear statistics (Optional)

Sidekiq::Stats.new.reset