How to clear all the jobs from Sidekiq?

98,500

Solution 1

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

require 'sidekiq/api'

Solution 2

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 3

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 4

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

redis-cli flushdb

You can also use redis-cli flushall

Share:
98,500

Related videos on Youtube

Can Can
Author by

Can Can

Updated on December 22, 2021

Comments

  • Can Can
    Can Can over 2 years

    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?

    • Benj
      Benj almost 10 years
      try include 'sidekiq' before
    • Can Can
      Can Can almost 10 years
      @BenjaminSinclaire its giving TypeError: wrong argument type String (expected Module)
    • Uri Agassi
      Uri Agassi almost 10 years
      try require 'sidekiq' before
    • Benj
      Benj almost 10 years
      I always (really always) confuse include and require :)
  • intcreator
    intcreator almost 7 years
    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?
  • jonathanccalixto
    jonathanccalixto almost 7 years
    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.
  • codemilan
    codemilan about 6 years
    OMG, what the heck is this ? @Xavier, is there method named .💣, can you please guide me the doc on this method (💣).
  • Modus Tollens
    Modus Tollens about 6 years
    @codemilan See this blog post: blog.honeybadger.io/…
  • M. Habib
    M. Habib about 6 years
    This should be the accepted answer considering its votes.
  • maurice
    maurice about 6 years
    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.
  • Charles Skariah
    Charles Skariah almost 6 years
    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
    Charles Skariah almost 6 years
    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.
  • jonathanccalixto
    jonathanccalixto almost 6 years
    @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. ;)
  • escanxr
    escanxr almost 5 years
    You can also clear all queues with Sidekiq::Queue.all.map(&:clear)... Useful when you sync your locale db with the prod 😅
  • meoww-
    meoww- over 4 years
    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.
  • Shankar Thyagarajan
    Shankar Thyagarajan about 4 years
    I got invalid byte sequence in US-ASCII (ArgumentError) on executing q.💣 :/
  • Ankit Deshpande
    Ankit Deshpande over 3 years
    Please add a disclaimer, this will flush redis, and clear any other keys present on the redis box
  • r4cc00n
    r4cc00n over 2 years
    Best answer ever!
  • Toucouleur
    Toucouleur about 2 years
    I love that code!