Clear sidekiq queue

Ruby on-RailsSidekiq

Ruby on-Rails Problem Overview


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.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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 - Ruby on-Rails

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 - Ruby on-Rails

Works for me for most sidekiq versions:

Sidekiq::RetrySet.new.clear

Sidekiq::ScheduledSet.new.clear

Clear statistics (Optional)

Sidekiq::Stats.new.reset

Solution 4 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLinus OleanderView Question on Stackoverflow
Solution 1 - Ruby on-RailsRanjithkumar RaviView Answer on Stackoverflow
Solution 2 - Ruby on-RailsiGELView Answer on Stackoverflow
Solution 3 - Ruby on-RailsrusllonrailsView Answer on Stackoverflow
Solution 4 - Ruby on-RailsAsad HameedView Answer on Stackoverflow