Skip to content

Instantly share code, notes, and snippets.

@richardking
Created February 19, 2014 22:17
Show Gist options
  • Select an option

  • Save richardking/9102847 to your computer and use it in GitHub Desktop.

Select an option

Save richardking/9102847 to your computer and use it in GitHub Desktop.
twitter delete script
trap(:SIGHUP) { reload }
@@stagger = 30
def reload
path = "stagger.txt"
if File.exist?(path)
@@stagger = File.read(path).strip.to_i
puts "@@stagger reloaded with #{@@stagger}"
else
puts "no stagger.txt file found, keeping @@stagger at #{@@stagger}"
end
end
def execute(sql)
Tweet.connection.execute(sql)
end
# curl for newrelic api
`curl -H "x-api-key:b2751da5c301d17265b23fba34da4cd7829b9120b46e96d" -d "deployment[application_id]=2086197" -d "deployment[description]=Starting tweets delete" -d "deployment[user]=Richard King" https://api.newrelic.com/deployments.xml`
begin
Tweet.find_in_batches(:conditions => ["created_at < ?", 1.month.ago]) do |batch|
puts "batch left #{batch.first.id} right #{batch.last.id}"
ids = batch.collect(&:id).join(',')
execute("DELETE FROM tweet_tags WHERE tweet_id IN (#{ids})")
execute("DELETE FROM tweets WHERE id IN (#{ids})")
if @@stagger > 0
puts("Staggering: delaying for #{@@stagger} seconds before next batch insert")
sleep(@@stagger)
end
end
rescue Exception => e
`curl -H "x-api-key:b2751da5c301d17265b23fba34da4cd7829b9120b46e96d" -d "deployment[application_id]=2086197" -d "deployment[description]=Ending tweets delete" -d "deployment[user]=Richard King" https://api.newrelic.com/deployments.xml`
exit
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment