How to clear all the jobs from Sidekiq?

Ruby on-RailsRubySidekiq

Ruby on-Rails Problem Overview


I am using sidekiq for background tasks in Rails application. Now the numbers of jobs becomes more, so I want to clear all the jobs. I tried the following command in console

Sidekiq::Queue.new.clear

but it was giving following error.

NameError: uninitialized constant Sidekiq::Queue 

How do I clear all the jobs from sidekiq?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You can do as it says on the issue 1077 or as reported in this blog at noobsippets

Both suggest we do the following, and can be done on rails console:

Sidekiq.redis(&:flushdb)

Caution: This command will clear all redis records. I suggest not using it in production

another approach would be

redis-cli --scan --pattern users: * | xargs redis-cli del

according to this blog

Solution 2 - Ruby on-Rails

Clear Sidekiq Jobs commands:

require 'sidekiq/api'

# Clear retry set

Sidekiq::RetrySet.new.clear

# Clear scheduled jobs 

Sidekiq::ScheduledSet.new.clear

# Clear 'Dead' jobs statistics

Sidekiq::DeadSet.new.clear

# Clear 'Processed' and 'Failed' jobs statistics

Sidekiq::Stats.new.reset

# Clear all queues

Sidekiq::Queue.all.map(&:clear)

# Clear specific queue

stats = Sidekiq::Stats.new
stats.queues
# => {"main_queue"=>25, "my_custom_queue"=>1}

queue = Sidekiq::Queue.new('my_custom_queue')
queue.count
queue.clear

Solution 3 - Ruby on-Rails

According to this issue on Github: https://github.com/mperham/sidekiq/issues/1732 you now need to

require 'sidekiq/api'

Solution 4 - Ruby on-Rails

As of latest Sidekiq, just blow it up:

require 'sidekiq/api'

q = Sidekiq::Queue.new
q.💣

Yes, the command to clear all is literally a bomb emoji. Also works for Sidekiq::RetrySet.

Or if you're no fun you can use q.clear

Solution 5 - Ruby on-Rails

redis-cli flushdb

You can also use redis-cli flushall

Solution 6 - Ruby on-Rails

Use Rails runner in one line

rails runner 'Sidekiq.redis { |conn| conn.flushdb }'

Solution 7 - Ruby on-Rails

You can use this for clearing all the jobs

require 'sidekiq/api'

Sidekiq::Queue.all.each(&:clear)

Solution 8 - Ruby on-Rails

require 'sidekiq/api'

Sidekiq::Queue.all.each {|x| x.clear}

Solution 9 - Ruby on-Rails

All Sidekiq tasks are saved in "Redis".

You can clean "Redis" by this command

redis-cli flushall

Solution 10 - Ruby on-Rails

If you want to delete jobs from specific queues try:

queue = Sidekiq::Queue.new("default")
queue.each do |job|
  job.klass # => 'TestWorker'
  job.args # => ['easy']
  job.delete if job.jid == 'abcdef1234567890' || job.klass == 'TestWorker'
end

Read all about sidekiq and important console commands- https://medium.com/@shashwat12june/all-you-need-to-know-about-sidekiq-a4b770a71f8f

Solution 11 - Ruby on-Rails

I realized that Sidekiq.redis { |conn| conn.flushdb } removes all keys from the redis database. There is a safer way to clear all sidekiq queues using redis-cli:

redis-cli keys "*queue:*" | xargs redis-cli del

The same can be achieved with Sidekiq API (see Ravi Prakash Singh answer)

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
QuestionCan CanView Question on Stackoverflow
Solution 1 - Ruby on-RailsjonathanccalixtoView Answer on Stackoverflow
Solution 2 - Ruby on-RailsrusllonrailsView Answer on Stackoverflow
Solution 3 - Ruby on-RailsJayView Answer on Stackoverflow
Solution 4 - Ruby on-RailsXavierView Answer on Stackoverflow
Solution 5 - Ruby on-RailsSai Ram ReddyView Answer on Stackoverflow
Solution 6 - Ruby on-RailsfangxingView Answer on Stackoverflow
Solution 7 - Ruby on-RailsRavi Prakash SinghView Answer on Stackoverflow
Solution 8 - Ruby on-RailsNicolás Schmidt GubbinsView Answer on Stackoverflow
Solution 9 - Ruby on-RailsAndriy KondzolkoView Answer on Stackoverflow
Solution 10 - Ruby on-Railsshashwat srivastavaView Answer on Stackoverflow
Solution 11 - Ruby on-RailsHirurg103View Answer on Stackoverflow