Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clear Sidekiq Jobs
require 'sidekiq/api'
# 1. Clear retry set
Sidekiq::RetrySet.new.clear
# 2. Clear scheduled jobs
Sidekiq::ScheduledSet.new.clear
# 3. Clear 'Processed' and 'Failed' jobs
Sidekiq::Stats.new.reset
# 3. Clear 'Dead' jobs statistics
Sidekiq::DeadSet.new.clear
# Stats
stats = Sidekiq::Stats.new
stats.queues
# {"production_mailers"=>25, "production_default"=>1}
# Queue
queue = Sidekiq::Queue.new('queue_name')
queue.count
queue.clear
queue.each { |job| job.item } # hash content
# Redis Acess
Sidekiq.redis { |redis| redis.keys }
# ["stat:queues"...
@TatianaTyu

This comment has been minimized.

Copy link

@TatianaTyu TatianaTyu commented Jan 21, 2019

Thank you!

@jimmychu0807

This comment has been minimized.

Copy link

@jimmychu0807 jimmychu0807 commented Feb 8, 2019

This gist is such a helper. Thanks.

@jamesst20

This comment has been minimized.

Copy link

@jamesst20 jamesst20 commented Feb 22, 2019

Thank you.

As a side note, require 'sidekiq/api' must be first if you are in rails console

@magedmakled

This comment has been minimized.

Copy link

@magedmakled magedmakled commented Nov 20, 2019

Thanks James for the require 'sidekiq/api

@GesJeremie

This comment has been minimized.

Copy link

@GesJeremie GesJeremie commented Nov 24, 2019

Thank you.

@yanikj

This comment has been minimized.

Copy link

@yanikj yanikj commented Dec 30, 2019

Arigato!

@npc-adrian

This comment has been minimized.

Copy link

@npc-adrian npc-adrian commented Apr 20, 2020

Sidekiq::Queue.all.each {|q| q.clear} is a handy way to clear all queues

@DaniPB

This comment has been minimized.

Copy link

@DaniPB DaniPB commented May 26, 2020

I'm trying to test this with rspec, but the Sidekiq::RetrySet.new.clear is not working in test environment even when i did require 'sidekiq/api' in the test.

Do you know why?

@wbotelhos

This comment has been minimized.

Copy link
Owner Author

@wbotelhos wbotelhos commented May 26, 2020

Hi @DaniPB

Here the command runs on test env, but I think its not a good idea to use the real Sidekiq on test.
Maybe you should use rspec-sidekiq to simulate it.
But if you still need it, check the Test section of Sidekiq, maybe you should to use other requires.

Hope you could done the job. :)

@DaniPB

This comment has been minimized.

Copy link

@DaniPB DaniPB commented May 26, 2020

Hi @wbotelhos

Thanks for answer!

I'm using rspec-sidekiq and i can do jobs expectations, nevertheless, when I clear the scheduled jobs with Sidekiq::RetrySet, they still scheduled, and this is why:

image

I'm trying with this Sidekiq::Testing.disable! but for me is not a good idea and anyway is not working yet.

@wbotelhos

This comment has been minimized.

Copy link
Owner Author

@wbotelhos wbotelhos commented May 27, 2020

I see @DaniPB,

Maybe you can mock Redis and do the chances there, if Sidekiq writes there.

https://github.com/sds/mock_redis
https://github.com/guilleiguaran/fakeredis

@DaniPB

This comment has been minimized.

Copy link

@DaniPB DaniPB commented May 27, 2020

Yes! @wbotelhos that's a good idea
I was doing something wrong with Sidekiq::Testing.disable! i fixed and now works

image

Thanks a lot for u help!

@wbotelhos

This comment has been minimized.

Copy link
Owner Author

@wbotelhos wbotelhos commented May 27, 2020

Great, @DaniPB! 👏

I loved your editor hearts ^^

@DaniPB

This comment has been minimized.

Copy link

@DaniPB DaniPB commented May 27, 2020

Great, @DaniPB!

I loved your editor hearts ^^

@wbotelhos

Thank you!

It's my cute vim config heh!

@fedgut

This comment has been minimized.

Copy link

@fedgut fedgut commented Mar 18, 2021

This is amazing! thank you

@mfalgorepublic

This comment has been minimized.

Copy link

@mfalgorepublic mfalgorepublic commented Jun 16, 2021

thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment