136
votes

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?

11
try include 'sidekiq' before - Benj
@BenjaminSinclaire its giving TypeError: wrong argument type String (expected Module) - Can Can
try require 'sidekiq' before - Uri Agassi
I always (really always) confuse include and require :) - Benj

11 Answers

65
votes

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

require 'sidekiq/api'
233
votes

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

93
votes

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 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
39
votes

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

25
votes
redis-cli flushdb

You can also use redis-cli flushall

13
votes

Use Rails runner in one line

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

You can use this for clearing all the jobs

require 'sidekiq/api'

Sidekiq::Queue.all.each(&:clear)
12
votes
require 'sidekiq/api'

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

All Sidekiq tasks are saved in "Redis".

You can clean "Redis" by this command

redis-cli flushall
2
votes

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

0
votes

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)