Skip to content

Instantly share code, notes, and snippets.

@selcukcihan
Created January 5, 2024 08:20
Show Gist options
  • Save selcukcihan/359142a20aa5e8abf80578f085b21a6a to your computer and use it in GitHub Desktop.
Save selcukcihan/359142a20aa5e8abf80578f085b21a6a to your computer and use it in GitHub Desktop.
Script to delete tweets using the twitter data export output file
/*
* Usage: node delete_my_tweets.js {TWITTER_DATA_EXPORT}/data/tweets.js
* Set your twitter api keys before running the script on these env variables:
* TWITTER_CONSUMER_APP_KEY
* TWITTER_CONSUMER_APP_SECRET
* TWITTER_ACCESS_TOKEN
* TWITTER_ACCESS_SECRET
*/
import { TwitterApi } from "twitter-api-v2";
import fs from "fs";
const DELETED_TWEETS_FILE = "deleted.json";
const deletedTweets = fs.existsSync(DELETED_TWEETS_FILE)
? JSON.parse(fs.readFileSync(DELETED_TWEETS_FILE, "utf8"))
: [];
const client = new TwitterApi({
appKey: process.env.TWITTER_CONSUMER_APP_KEY,
appSecret: process.env.TWITTER_CONSUMER_APP_SECRET,
accessToken: process.env.TWITTER_ACCESS_TOKEN,
accessSecret: process.env.TWITTER_ACCESS_SECRET,
});
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const deleteTweet = async (tweetId) => {
try {
const response = await client.v2.deleteTweet(tweetId);
console.log(response);
if (response?.data?.deleted) {
deletedTweets.push(tweetId);
await delay(2000);
return true;
} else {
return false;
}
} catch (e) {
console.error(e);
return false;
}
};
async function run() {
const tweetsJs = fs.readFileSync(process.argv[2], "utf8");
const tweets = JSON.parse(tweetsJs.replace("window.YTD.tweets.part0 = ", ""));
let i = 0;
for (const tweet of tweets) {
console.log("Tweet", i, tweet.tweet.id_str, tweet.tweet.favorite_count);
i++;
if (parseInt(tweet?.tweet?.favorite_count || "0") > 300) {
continue;
}
const tweetId = tweet.tweet.id_str;
if (!deletedTweets.includes(tweetId)) {
const deleted = await deleteTweet(tweetId);
if (!deleted) {
console.error("Failed to delete tweet", tweetId);
break;
}
}
}
fs.writeFileSync(
DELETED_TWEETS_FILE,
JSON.stringify(deletedTweets, null, 2),
"utf8"
);
}
run();
@selcukcihan
Copy link
Author

selcukcihan commented Jan 5, 2024

Prerequisites

  1. You need to set up twitter developer account, follow https://developer.twitter.com/en/docs/twitter-api/getting-started/getting-access-to-the-twitter-api

  2. Export your API keys before running the script:

export TWITTER_CONSUMER_APP_KEY=
export TWITTER_CONSUMER_APP_SECRET=
export TWITTER_ACCESS_TOKEN=
export TWITTER_ACCESS_SECRET=
  1. You also need to download your tweets from https://twitter.com/settings/download_your_data

  2. When you're all set up, run node delete_my_tweets.js {TWITTER_DATA_EXPORT}/data/tweets.js

You can tweak the filter to select which tweets to be deleted, current filter reads: parseInt(tweet?.tweet?.favorite_count || "0") > 300 which implies "do not delete tweets with more than 300 likes".

@cerohazuri
Copy link

Hello,

wanted to ask how I can get the JS to work? Do I have to start it in the Chrome Console? If so, where do I have to put the file?
Tesekürler! :)

@selcukcihan
Copy link
Author

Hi, you run it with node on the terminal (step 4 above).

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