Skip to content

Instantly share code, notes, and snippets.

@sheharyarn
Last active April 15, 2020 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheharyarn/26a36816002ac7f5e14e0243fc0a3c63 to your computer and use it in GitHub Desktop.
Save sheharyarn/26a36816002ac7f5e14e0243fc0a3c63 to your computer and use it in GitHub Desktop.
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