165

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?

Rimian
  • 36,864
  • 16
  • 117
  • 117
Can Can
  • 3,644
  • 5
  • 32
  • 56

12 Answers12

281

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

adriancm
  • 5
  • 2
jonathanccalixto
  • 2,980
  • 2
  • 14
  • 10
  • This appears to reset my authentication on the Rails apps that I'm using (which means I have to copy cookies back into my HTTP client). Is there a way to prevent that? – intcreator Jun 13 '17 at 19:47
  • 2
    Hello, Brandeamon. Are you using redis to control the session of your project? Because these commands are to "clean" all data stored in redis, it is equivalent to a drop database or drop table in relational databases. – jonathanccalixto Jun 18 '17 at 18:49
  • 3
    This should be the accepted answer considering its votes. – M. Habib Apr 26 '18 at 12:52
  • This works more predictably than `redis-cli flushdb` if you are running redis on a separate server from your rails server. I was wondering why `redis-cli flushdb` didn't work until I remembered I needed to include host and port arguments. – maurice May 08 '18 at 21:32
  • 4
    The problem with this approach is if you are sharing same redis/elasticache across different services, it's gonna clear all the other data in the redis db as well. – Charles Skariah May 31 '18 at 05:39
  • @Charles Really, but it has how to clean specific parts of the redis, I will research on the subject and post here. But as I use this only in development, for me the solution resolves. ;) – jonathanccalixto Jun 01 '18 at 11:23
  • 1
    This was the only approach that we found would work for ensuring that all recurring job changes would be picked up. We run this every time we do a build as part of our automated processes. We also have a secured mechanism for accessing this and re-daemonizing Sidekiq via one of our application's APIs (in the case of the need for a hot-reload). As far as this flushing other redis instances, we have a separate instance of redis through docker running exclusively for Sidekiq, so data contamination/loss isn't an issue for our group. Thanks for posting this. – meoww- Sep 01 '19 at 16:41
  • 1
    Please add a disclaimer, this will flush redis, and clear any other keys present on the redis box – Ankit Deshpande Oct 29 '20 at 14:51
127

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
wscourge
  • 10,657
  • 14
  • 59
  • 80
rusllonrails
  • 5,586
  • 3
  • 34
  • 27
66

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

require 'sidekiq/api'
Jay
  • 930
  • 7
  • 11
47

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

Xavier
  • 3,423
  • 23
  • 36
26
redis-cli flushdb

You can also use redis-cli flushall

Sunny
  • 5,825
  • 2
  • 31
  • 41
Sai Ram Reddy
  • 1,079
  • 13
  • 14
  • 6
    The problem with this approach is if you are sharing same redis/elasticache across different services, it's gonna clear all the other data in the db as well. – Charles Skariah May 31 '18 at 05:37
19

Use Rails runner in one line

rails runner 'Sidekiq.redis { |conn| conn.flushdb }'
Fangxing
  • 5,716
  • 2
  • 49
  • 53
16

You can use this for clearing all the jobs

require 'sidekiq/api'

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

Sidekiq::Queue.all.each {|x| x.clear}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
13

All Sidekiq tasks are saved in "Redis".

You can clean "Redis" by this command

redis-cli flushall
Andriy Kondzolko
  • 802
  • 10
  • 13
2

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

2

While many ways for doing this are pretty well-documented by other answers here, Sidekiq::Queue#clear can't execute when you're running into Redis::CommandError (OOM command not allowed when used memory > 'maxmemory'.) When this happens, you can still individually delete records from your problem queue until there is enough memory for the clear method to work. For example, this worked for me in a Rails console:

problem_queue_name = 'my_big_queue'
queue = Sidekiq::Queue.new(problem_queue_name)
# Alternate between deleting jobs in a batch and attempting clear until it succeeds
queue.first(10000).each do |q| print('.') if q.delete end; nil
queue.clear
Allison
  • 1,925
  • 1
  • 18
  • 25
1

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)

Hirurg103
  • 4,783
  • 2
  • 34
  • 50