Skip to content

Instantly share code, notes, and snippets.

@marksim
Created April 17, 2009 11:37
Show Gist options
  • Save marksim/96985 to your computer and use it in GitHub Desktop.
Save marksim/96985 to your computer and use it in GitHub Desktop.
#Method to slowly delete items from a db giving sleep time between deletions to prevent longer term locking
def slow_delete(num1, num2=nil, options={})
step = options[:step] || 5000
sleeptime = options[:sleep] || 2
klazz = options[:class] || Log
if num2.nil?
num2 = num1
num1 = 0
end
m = num1+step
t = 0
while m < num2
t += klazz.delete_all("id < #{m} and id >= #{m-step}")
m += step
print "."
sleep sleeptime
end
t += klazz.delete_all("id < #{num2} and id >= #{m}")
t
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment