Skip to content

Instantly share code, notes, and snippets.

@adamcrown
Last active December 11, 2015 00:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcrown/4515444 to your computer and use it in GitHub Desktop.
Save adamcrown/4515444 to your computer and use it in GitHub Desktop.
Deleting a large batch of session records can really overload the DB (at least MySQL). This script paces it out a bit.
def batch_session_prune(keep_after = 1.month.ago, batch_by = 1.day, pause_time = 10)
time = ActiveRecord::SessionStore::Session.order(:updated_at).first[:updated_at]
while time < keep_after
time = time + batch_by
puts "Deleting sessions from #{time.to_s(:short)}"
ActiveRecord::SessionStore::Session.where('updated_at < ?', time).delete_all
sleep pause_time
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment