Skip to content

Instantly share code, notes, and snippets.

@sbolel
Last active October 19, 2024 15:05
Show Gist options
  • Save sbolel/a2b2bfde16b3ab185fbc2e2049240abc to your computer and use it in GitHub Desktop.
Save sbolel/a2b2bfde16b3ab185fbc2e2049240abc to your computer and use it in GitHub Desktop.
Instagram Comment Activity Deleter: Automate the deletion of all your Instagram comments from the 'Your Activity' section. Perfect for quick digital clean-up.
/**
* This script automates the process of deleting your own Instagram comments.
* It deletes comments in batches to avoid hitting rate limits or breaking the page.
*
* WARNING: This function directly manipulates the DOM and depends on the current HTML
* structure of Instagram's website to work. If Instagram implements changes to the
* activity page layout, structure, or functionality, this script may break or cause
* unexpected behavior. Use at your own risk and always review code before running it.
*
* How to use:
* 1. Navigate to the Instagram comments page by going to:
* https://www.instagram.com/your_activity/interactions/comments
* 2. Open the developer console in your web browser:
* - Chrome/Firefox: Press Ctrl+Shift+J (Windows/Linux) or Cmd+Option+J (Mac)
* - Safari: Enable the Develop menu in Safari's Advanced preferences, then press Cmd+Option+C
* 3. Copy and paste this entire script into the console and press Enter to run it.
*
* How to navigate to the comments page on instagram.com:
* 1. Log in to Instagram on a desktop browser.
* 2. Go to your profile by clicking on the profile icon at the bottom right.
* 3. Click on "Your Activity" in the menu.
* 4. Select "Interactions" and then "Comments".
* 5. Follow the usage steps above to run this script.
*/
;(async function () {
// Constants
/** @const {number} - The number of comments to delete in each batch. */
const DELETION_BATCH_SIZE = 3
/** @const {number} - The delay between actions in milliseconds. */
const DELAY_BETWEEN_ACTIONS_MS = 1000
/** @const {number} - The delay between clicking the checkboxes in milliseconds. */
const DELAY_BETWEEN_CHECKBOX_CLICKS_MS = 300
/** @const {number} - The maximum number of retries for waiting operations */
const MAX_RETRIES = 60
/**
* Utility function that delays execution for a given amount of time.
* @param {number} ms - The milliseconds to delay.
* @returns {Promise<void>} A promise that resolves after the specified delay.
*/
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
/**
* Utility function that waits for an element to appear in the DOM before resolving.
* @param {string} selector - The CSS selector of the element to wait for.
* @param {number} [timeout=30000] - The maximum time to wait in milliseconds.
* @returns {Promise<Element>} A promise that resolves with the found element.
* @throws {Error} If the element is not found within the timeout period.
*/
const waitForElement = async (selector, timeout = 30000) => {
const startTime = Date.now()
while (Date.now() - startTime < timeout) {
const element = document.querySelector(selector)
if (element) return element
await delay(100)
}
throw new Error(`Element with selector "${selector}" not found within ${timeout}ms`)
}
/**
* Utility function that clicks on a given element.
* @param {Element} element - The element to click.
* @throws {Error} If the element is not found.
*/
const clickElement = async (element) => {
if (!element) throw new Error('Element not found')
element.click()
}
/**
* Waits for the "Select" button to reappear after the page loads more comments
* following the deletion of a batch of comments when the "Select" button
* is hidden while a spinner indicates that more comments are loading.
* @returns {Promise<void>} A promise that resolves when the select button reappears.
* @throws {Error} If the select button is not found after maximum retries.
*/
const waitForSelectButton = async () => {
for (let i = 0; i < MAX_RETRIES; i++) {
const buttonCount = document.querySelectorAll('[role="button"]')?.length
if (buttonCount > 1) return
await delay(1000)
}
throw new Error('Select button not found after maximum retries')
}
/**
* Deletes the currently selected comments.
* @returns {Promise<void>} A promise that resolves when the comments are deleted.
*/
const deleteSelectedComments = async () => {
try {
const deleteButton = await waitForElement('[aria-label="Delete"]')
await clickElement(deleteButton)
await delay(DELAY_BETWEEN_ACTIONS_MS)
const confirmButton = await waitForElement('button[tabindex="0"]')
await clickElement(confirmButton)
} catch (error) {
console.error('Error during comment deletion:', error.message)
}
}
/**
* Deletes all user comments by selecting comments in batches.
* @returns {Promise<void>} A promise that resolves when all comments are deleted.
*/
const deleteActivity = async () => {
try {
while (true) {
const [, selectButton] = document.querySelectorAll('[role="button"]')
if (!selectButton) throw new Error('Select button not found')
await clickElement(selectButton)
await delay(DELAY_BETWEEN_ACTIONS_MS)
const checkboxes = document.querySelectorAll('[aria-label="Toggle checkbox"]')
if (checkboxes.length === 0) {
console.log('No more comments to delete')
break
}
for (let i = 0; i < Math.min(DELETION_BATCH_SIZE, checkboxes.length); i++) {
await clickElement(checkboxes[i])
await delay(DELAY_BETWEEN_CHECKBOX_CLICKS_MS)
}
await delay(DELAY_BETWEEN_ACTIONS_MS)
await deleteSelectedComments()
await delay(DELAY_BETWEEN_ACTIONS_MS)
await waitForSelectButton()
await delay(DELAY_BETWEEN_ACTIONS_MS)
}
} catch (error) {
console.error('Error in deleteActivity:', error.message)
}
}
// Start the deletion process
try {
await deleteActivity()
console.log('Activity deletion completed')
} catch (error) {
console.error('Fatal error:', error.message)
}
})()
@HBramley
Copy link

HBramley commented Aug 7, 2024

@cybr47 It won't automatically delete the comments for me. It selects 10, gives me like 3 seconds to press delete and if I don't it just unselects all of them then loops

@65156
Copy link

65156 commented Aug 13, 2024

same issue, deletes 40, but then will show an error and break out of the script.

@sbolel
Copy link
Author

sbolel commented Aug 14, 2024

@cybr47 @mcrawleypilot @HBramley @65156 Please try out the newest version above. I've updated the script to work with the reloading of the activity log after the deletion. It now waits for the spinner to go away before continuing to select and delete activity.

Change the value of the DELETION_BATCH_SIZE const to increase the number of comments deleted per batch. I set it to 3 for testing. Seems to work well.

Hope that helps!

@sadhorse79
Copy link

sadhorse79 commented Aug 19, 2024

I have this error on Firefox

Uncaught (in promise) TypeError: document.querySelector(...) is null
    async* debugger eval code:62
[debugger eval code:47:12](chrome://devtools/content/webconsole/debugger%20eval%20code)
    deleteAllUserComments debugger eval code:47
    InterpretGeneratorResume self-hosted:1417
    AsyncFunctionNext self-hosted:804
    (Asenkron: async)
    <anonim> debugger eval code:62
    getEvalResult resource://devtools/server/actors/webconsole/eval-with-debugger.js:306
    evalWithDebugger resource://devtools/server/actors/webconsole/eval-with-debugger.js:218
    evaluateJS resource://devtools/server/actors/webconsole.js:953
    evaluateJSAsync resource://devtools/server/actors/webconsole.js:846
    makeInfallible resource://devtools/shared/ThreadSafeDevToolsUtils.js:103

@sbolel
Copy link
Author

sbolel commented Aug 20, 2024

@sadhorse79 I was able to reproduce the issue in Firefox, thanks for the heads up. I updated the gist with refactored code that fixes the issue. Please give it another shot!

@sadhorse79
Copy link

sadhorse79 commented Aug 20, 2024

buddy now it says. and does not delete

No more comments to delete [debugger eval code:117:19]
Activity deletion completed [debugger eval code:140:13]

@sbolel
Copy link
Author

sbolel commented Aug 20, 2024

@sadhorse79 not sure what's going on there without debugging what you're seeing.

Is it correctly clicking the "select" button and do you see checkboxes on your page before it fails with that message?

@sadhorse79
Copy link

sadhorse79 commented Aug 27, 2024

it clicks on select button and checkboxes appearing. but nothing more happens.

No more comments to delete [debugger eval code:93:19]
Activity deletion completed [debugger eval code:116:13]

@drHyperion451
Copy link

It would be cool to add some sort of a username whitelist to the script, so I can just delete the comments from people I don't follow.

@vineyrawat
Copy link

Thanks! Bro

@digitalscrubber
Copy link

digitalscrubber commented Sep 11, 2024

Thank you! It works on the comments and story responses but not on the likes fyi--it does some weird hang.
I wish i could unsend all my chat messages. It's hard scrubbing the internet.

@gerwld
Copy link

gerwld commented Sep 12, 2024

I'll add some context just in case you got the same error as I had - No more comments to delete. You have to switch your interface language to english. Otherways it wouldn't work because of the querySelectors values. Hope it helps someone.

By the way, huge thank you for that script, sbolel!

@buncho12
Copy link

buncho12 commented Sep 12, 2024

Thanks for the script, but having some issues on the latest Firefox build - The script would finish without pressing the confirmation button, which I fixed by just changing the argument in line 95 to 'button._a9--._ap36._a9_1 div._ap3a._aacp._aacw._aac-._aad6'

However, it doesn't loop and breaks out of the script without the select button being shown.

@gerwld
Copy link

gerwld commented Sep 13, 2024

However, it doesn't loop and breaks out of the script without the select button being shown.

try to use absolute XPath, for me it fixed that problem

@viserys73
Copy link

it doesn't work for me i tried but it just give open the selection mode then it returns to viewing mode. can you give me the code with out any explanation like raw code?

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