Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Delete all of your tweets before a certain date
defmodule TwitterDeleter do
@tweet_data Path.expand("~/Downloads/twitterdump/assets/tweet.js")
@cutoff Date.from_iso8601!("2015-01-01")
@call_limit 3000
@chunk_no 3
require Logger
def run do
file = File.read!(@tweet_data)
data = Jason.decode!(file)
mapped = Enum.map(data, & %{&1 | "created_at" => parse_date(&1["created_at"]) })
sorted = Enum.sort_by(mapped, & &1["created_at"], &Timex.before?/2)
chunked = Enum.chunk_every(sorted, @call_limit)
sample = Enum.at(chunked, @chunk_no)
results =
sample
|> Enum.map(&delete/1)
|> Enum.group_by(&elem(&1, 0), &elem(&1, 1))
if results[:error] do
Logger.warn("Failed for these tweets:\n#{inspect(results[:error])}")
else
Logger.info("Successfully deleted all tweets")
end
end
def delete(%{"id" => id} = tweet) do
if Timex.before?(tweet["created_at"], @cutoff) do
ExTwitter.destroy_status(id)
Logger.info("Deleted tweet #{id}")
else
Logger.info("Skipping tweet #{id}")
end
{:ok, id}
rescue
error ->
Logger.error("Failed for #{id}: #{error.message}")
{:error, id}
end
def parse_date(date) do
Timex.parse!(date, "%a %b %d %T %z %Y", :strftime)
end
end

Create a new Elixir project, and add these depedendencies:

defp deps do
  [
    {:jason, "~> 1.1"},
    {:timex, "~> 3.1"},
    {:oauther, "~> 1.1"},
    {:extwitter, "~> 0.8"},
  ]
end

Update your config/config.exs with twitter creds:

config :extwitter, :oauth, [
  consumer_key: System.get_env("TWITTER_CONSUMER_KEY"),
  consumer_secret: System.get_env("TWITTER_CONSUMER_SECRET"),
  access_token: System.get_env("TWITTER_ACCESS_TOKEN"),
  access_token_secret: System.get_env("TWITTER_ACCESS_TOKEN_SECRET"),
]

Download and extract your twitter data dump, and update the module to point to its path. Start IEx and run:

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