After many attempts and using various solutions, I found the best one after many tries and with the help of ChatGPT. If you're looking to automate the deleting of all activities on X (Twitter), I found a great solution, thanks to Lionbytes. The script handles undoing reposts, retries, and scrolling to load more posts efficiently. Just press F12 in Chrome, choose the console, copy the code, and select the tab that needs to be removed.
Here's the link to the script: Lionbytes Undo Repost Script.
Script to Delete Tweets
//Run this script from https://twitter.com/{yourTwitterHandle}:
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const randomDelay = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
const deleteTweetsAndUndoReposts = async () => {
let actionsCount = 0;
const maxActions = 1000; // change this number depend on your number of posts
let retryCount = 0;
const maxRetries = 3;
const deleteTweet = async (button) => {
try {
button.click();
await sleep(randomDelay(100, 300));
let menuItems = document.querySelectorAll('div[role="menuitem"]');
let deleteOption = Array.from(menuItems).find(item => item.innerText.includes('Delete') || item.innerText.includes('삭제'));
if (deleteOption) {
deleteOption.click();
await sleep(randomDelay(300, 500));
let confirmDelete = document.querySelector('[data-testid="confirmationSheetConfirm"]');
if (confirmDelete) {
confirmDelete.click();
await sleep(randomDelay(300, 500));
actionsCount++;
console.log('Deleted tweet. Total actions:', actionsCount);
return true;
} else {
console.log('Confirmation button not found');
}
} else {
console.log('Delete option not found');
}
} catch (e) {
console.error('Error deleting tweet:', e);
}
return false;
};
const undoRepost = async (button) => {
try {
button.click();
await sleep(randomDelay(300, 500));
let confirmUndo = document.querySelector('[data-testid="unretweetConfirm"]');
if (confirmUndo) {
confirmUndo.click();
await sleep(randomDelay(300, 500));
actionsCount++;
console.log('Undid repost. Total actions:', actionsCount);
return true;
} else {
console.log('Undo Repost confirmation button not found');
}
} catch (e) {
console.error('Error undoing repost:', e);
}
return false;
};
while (actionsCount < maxActions) {
let deleteButtons = Array.from(document.querySelectorAll('[data-testid="caret"]'))
.filter(btn => btn.closest('article'));
let undoRepostButtons = Array.from(document.querySelectorAll('[data-testid="unretweet"]'))
.filter(btn => btn.closest('article'));
if (deleteButtons.length === 0 && undoRepostButtons.length === 0) {
window.scrollTo(0, document.body.scrollHeight);
await sleep(randomDelay(300, 500));
deleteButtons = Array.from(document.querySelectorAll('[data-testid="caret"]'))
.filter(btn => btn.closest('article'));
undoRepostButtons = Array.from(document.querySelectorAll('[data-testid="unretweet"]'))
.filter(btn => btn.closest('article'));
}
if (deleteButtons.length === 0 && undoRepostButtons.length === 0) break;
let actionPerformed = false;
for (let button of deleteButtons) {
if (actionsCount >= maxActions) break;
const success = await deleteTweet(button);
if (success) {
actionPerformed = true;
} else {
console.log('Failed to delete tweet, moving to next.');
}
}
for (let button of undoRepostButtons) {
if (actionsCount >= maxActions) break;
const success = await undoRepost(button);
if (success) {
actionPerformed = true;
} else {
console.log('Failed to undo repost, moving to next.');
}
}
if (!actionPerformed) {
retryCount++;
if (retryCount >= maxRetries) {
console.log('Too many retries without actions. Skipping problematic posts and scrolling.');
retryCount = 0;
window.scrollBy(0, 500);
await sleep(randomDelay(300, 500));
} else {
console.log('Retrying to find actions. Attempt:', retryCount);
await sleep(1500);
}
} else {
retryCount = 0;
}
}
console.log('Stopped after performing', actionsCount, 'delete/undo repost actions');
};
deleteTweetsAndUndoReposts();
Script to Delete Replies
//Run this script from https://twitter.com/{yourTwitterHandle}/with_replies:
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const randomDelay = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
const scrollToBottom = async () => {
window.scrollTo(0, document.body.scrollHeight);
await sleep(randomDelay(500, 1000)); // Adjust sleep time if needed
};
const deleteReplies = async () => {
let actionsCount = 0;
const maxActions = 1000; // Change this number based on your number of replies
const deleteReply = async (button) => {
try {
button.click();
await sleep(randomDelay(100, 300));
let menuItems = document.querySelectorAll('div[role="menuitem"]');
let deleteOption = Array.from(menuItems).find(item => item.innerText.includes('Delete') || item.innerText.includes('삭제'));
if (deleteOption) {
deleteOption.click();
await sleep(randomDelay(300, 500));
let confirmDelete = document.querySelector('[data-testid="confirmationSheetConfirm"]');
if (confirmDelete) {
confirmDelete.click();
await sleep(randomDelay(300, 500));
actionsCount++;
console.log('Deleted reply. Total actions:', actionsCount);
return true;
} else {
console.log('Confirmation button not found');
}
} else {
console.log('Delete option not found');
}
} catch (e) {
console.error('Error deleting reply:', e);
}
return false;
};
while (actionsCount < maxActions) {
let deleteButtons = Array.from(document.querySelectorAll('[data-testid="caret"]'))
.filter(btn => btn.closest('article'));
if (deleteButtons.length === 0) {
await scrollToBottom();
deleteButtons = Array.from(document.querySelectorAll('[data-testid="caret"]'))
.filter(btn => btn.closest('article'));
}
if (deleteButtons.length === 0) break;
for (let button of deleteButtons) {
if (actionsCount >= maxActions) break;
await deleteReply(button);
}
await scrollToBottom(); // Scroll again to load more replies
}
console.log('Stopped after performing', actionsCount, 'delete actions');
};
deleteReplies();
Script to Unlike Tweets
//Run this script from https://twitter.com/{yourTwitterHandle}/likes:
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const randomDelay = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
const unlikeTweets = async () => {
let actionsCount = 0;
const maxActions = 1000; // change this number based on your number of likes
let lastScrollHeight = 0;
const unlikeTweet = async (button) => {
try {
button.click();
await sleep(randomDelay(300, 500));
actionsCount++;
console.log('Unliked tweet. Total actions:', actionsCount);
return true;
} catch (e) {
console.error('Error unliking tweet:', e);
}
return false;
};
while (actionsCount < maxActions) {
let unlikeButtons = Array.from(document.querySelectorAll('[data-testid="unlike"]'))
.filter(btn => btn.closest('article'));
if (unlikeButtons.length === 0) {
window.scrollTo(0, document.body.scrollHeight);
await sleep(randomDelay(300, 500));
unlikeButtons = Array.from(document.querySelectorAll('[data-testid="unlike"]'))
.filter(btn => btn.closest('article'));
}
if (unlikeButtons.length === 0) {
const newScrollHeight = document.body.scrollHeight;
if (newScrollHeight === lastScrollHeight) {
console.log('No more tweets to unlike.');
break;
}
lastScrollHeight = newScrollHeight;
window.scrollTo(0, document.body.scrollHeight);
await sleep(randomDelay(300, 500));
continue;
}
for (let button of unlikeButtons) {
if (actionsCount >= maxActions) break;
await unlikeTweet(button);
await sleep(10000); // Delay to avoid Twitter noticing
}
window.scrollTo(0, document.body.scrollHeight);
await sleep(randomDelay(300, 500));
}
console.log('Stopped after performing', actionsCount, 'unlike actions');
};
unlikeTweets();
setTimeout(delButton, 1000)
etc.nestedTimeouts=()=>{ setTimeout(delButton,1000, setTimeout(clickDel,1000)); }