Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// This is a hack, a quick and dirty console script for RT/tweets (with replies) removal w/o API
// To be used in: https://twitter.com/Username/with_replies
// Set your username (without @) below (case-sensitive) to correctly trigger the right Menu
const tweetUser = 'Username'
// BUG, With above we still trigger Menu on some replies but relatively harmless.
// @Hack Implement simple has() for querySelector
const querySelectorHas = function( parent, child ){
return [].filter.call( document.querySelectorAll( parent ), function( elem ){
if(elem.querySelector( child ) !== null ) {
return true
}
else {
return false
}
});
}
// @Hack Implement xpath text() selector returning matching holder element
// equiv XPath //find[text()='inner']
const querySelectorInner = function( parent, inner ){
return [].filter.call( document.querySelectorAll( parent ), function( elem ){
if(elem.innerHTML == inner ) {
return true
}
else {
return false
}
});
}
setInterval(() => {
console.log('--- Twitter Removal Clicks Round')
// For Old RT's may need (?) to RT and then Unretweet - Just watch for the UI rate limits
var unretweets = 0
for (const d of document.querySelectorAll('div[data-testid="unretweet"]')) {
unretweets++
d.click()
}
var unretweetconfirms = 0
for (const r of document.querySelectorAll('div[data-testid="unretweetConfirm"]')) {
unretweetconfirms++
r.click()
}
console.log('Unretweets: ' + unretweets + ', Confirms: ' + unretweetconfirms)
var clicks = 0
for(const d of querySelectorHas("div[data-testid='tweet']", "a[href='/" + tweetUser + "']")) {
const moreButton = d.querySelector("div[aria-label='More']")
clicks++
moreButton.click()
}
var clickDeletes = 0
for(const deleteButton of querySelectorInner("span", 'Delete')) {
deleteButton.click()
clickDeletes++
}
var clickConfirms = 0
for(const deleteConfirm of document.querySelectorAll("div[data-testid='confirmationSheetConfirm']")) {
deleteConfirm.click()
clickConfirms++
}
console.log('Menu: ' + clicks + ', Deletes: ' + clickDeletes + ', Confirms: ' + clickConfirms)
// Scrolling is more involved/difficult as twitter does not hide rest of the thread if reply deleted
// As work-around scroll yourself when removals hit zero. API would be best option but this solution is WEB UI driven.
window.scrollTo(0, document.body.scrollHeight)
}, 3000) // Run every 3s
/*******************************************************************************************
*
* THIS GIST IS PROVIDED "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL WE BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*******************************************************************************************/
@FocusWho

This comment has been minimized.

Copy link
Owner Author

@FocusWho FocusWho commented Jun 10, 2020

If you also want to delete Likes see:
https://gist.github.com/aymericbeaumet/d1d6799a1b765c3c8bc0b675b1a1547d

Deleting RT, replies and tweets is more involved on twitter REACT UI wise as there is

  1. A menu to toggle
  2. Delete button to press
  3. Modal Confirm
  4. Scrolling required (threads are a big mess)

Somebody wrote one earlier for this but it,

a) didn't delete multiple RT/tweets per interval run
b) was hard coded reliant on looking DOM div elements by ever-changing "obfuscated" classes strings therefore matching wrong things
c) broke execution chain (infinite recursion) if there was one unmatched element at any stage

This gist finds the correct menu elements (set your username so it finds your own tweets), runs from interval timer and catch-all-actions from window scroll point.

BUGS
i) Deleting replies is trickier since twitter UI leaves the underlying thread lying around so scrolling is not accurate - just scroll back up manually to hit some more.
ii) Manual scrolling back up may be required while interval function runs
iii) Does not detect twitter rate limit for deletions so you may need to re-run and maybe re-tweet and unretweet (?)

To stop the script just close the page as it runs in your console.
Yes I should have used XPath instead of two filter funcs but didn't think straight tonight 🙄

Also I left logging there to see what's going on and note it is Web2.0 thingy so even if twitter UI shows deleted it may not be the truth.

@KDReeves

This comment has been minimized.

Copy link

@KDReeves KDReeves commented Jun 16, 2020

This worked brilliantly and was exactly what I was looking for. Bravo, ma'am. Yes, I noticed that occasional scroll-up is necessary, but that is a far, far better situation than going one by one. This is nicely executed. I appreciate it, and will try your like script next! CHEERS!

@FocusWho

This comment has been minimized.

Copy link
Owner Author

@FocusWho FocusWho commented Jun 16, 2020

@KDReeves Thanks and much appreciated for your kind words :) 💁

@guihkx

This comment has been minimized.

Copy link

@guihkx guihkx commented Jul 15, 2020

const tweetUser = window.location.pathname.match(/^\/([^/]+)/)[1]
@mbproaz

This comment has been minimized.

Copy link

@mbproaz mbproaz commented Aug 12, 2020

I love the script, I tested it and it works great!
But could you also make a script that only unretweet retweets. This scripts deletes all tweets as well.
Thanks for your reply!

@aphirst

This comment has been minimized.

Copy link

@aphirst aphirst commented Sep 23, 2020

I too would love to see a working version that only removes retweets, not tweets.

@svpn

This comment has been minimized.

Copy link

@svpn svpn commented Sep 24, 2020

My "Tweets & replies" is empty but "Tweets" tab is full of retweets, and they don't get deleted.

Also there's no option manually to Undo Retweet, and if I Retweet again then Undo Retweet it seems to get removed but after refreshing the page they come back again.

@AtchadBoy

This comment has been minimized.

Copy link

@AtchadBoy AtchadBoy commented Oct 22, 2020

how use this

@eraykisabacak

This comment has been minimized.

Copy link

@eraykisabacak eraykisabacak commented Nov 30, 2020

Today, This code only un retweet working. But delete tweets not working

You haven't retweet under code running for delete tweets.

setInterval(() => {
  for (const d of document.querySelectorAll('div[data-testid="caret"]')) {
    d.click()
    document.querySelector('div[role="menu"] div[role="menuitem"]').click() 
    document.querySelector('div[role="alertdialog"] div[data-testid="confirmationSheetConfirm"]').click()
  }
  window.scrollTo(0, document.body.scrollHeight)
}, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.