Script to delete all tweets

This Ruby script is courtesy of @devonestes with a few minor edits from myself. Just posting it here in case anybody else needs it :003:

If you want to delete your tweets, it works pretty well…

require 'twitter'

puts "starting"
# Initial setup (don't change)
one_week_ago = (Time.now - 60 * 60 * 24 * 7)
one_month_ago = (Time.now - 60 * 60 * 24 * 31)
three_motnhs_ago = (Time.now - 60 * 60 * 24 * 31 * 3)
six_months_ago = (Time.now - 60 * 60 * 24 * 31 * 6)
one_year_ago = (Time.now - 60 * 60 * 24 * 31 * 12)
two_years_ago = (Time.now - 60 * 60 * 24 * 31 * 12 * 2)

# Set your username here
twitter_username = "YourUsernameHere"

# Set this for the tweets you _don't_ want deleted
older_than = one_year_ago

# Enter your credentials here
client = Twitter::REST::Client.new do |config|
  config.consumer_key        = "-key-here-"
  config.consumer_secret     = "-key-here-"
  config.access_token        = "-key-here-"
  config.access_token_secret = "-key-here-"
end

puts "logging in"
#
#
#
# Once the above has been set, run this script with `ruby /path/to/file`
#
#
#
# --------------------------------------------- #

def collect_with_max_id(collection=[], max_id=nil, &block)
  response = yield(max_id)
  collection += response
  response.empty? ? collection.flatten : collect_with_max_id(collection, response.last.id - 1, &block)
end
puts "collect_with_max_id"

def client.get_all_tweets(user)
  collect_with_max_id do |max_id|
    options = {count: 200, include_rts: true}
    options[:max_id] = max_id unless max_id.nil?
    user_timeline(user, options)
  end
end
puts "Client.get_all_tweets"

all_tweets = client.get_all_tweets(twitter_username)
puts "get_all_tweets"
puts "all tweets #{all_tweets.size}"

tweets_to_delete = all_tweets.select do |tweet|
  tweet.created_at < older_than
end
puts tweets_to_delete.size
puts "tweets_to_delete"

puts "start destroy_status"
client.destroy_status(tweets_to_delete)
puts "end destroy_status"
1 Like

I’m sorry. I’m out of practice with Ruby. Can you do the same with Elixir, which is what I come to this forum for? :grin:

That’s why it’s in the ‘other languages’ section :wink:

I haven’t had a need to look at the Twitter package for Elixir, but I’m pretty sure you could do it with Elixir :023:

You should edit each tweet three times, then empty it out with . - THEN delete it, in case Twitter uses soft delete, which let’s face it they do.

You can’t edit tweets :101: it’s delete or nothing :lol: