Forked from sbolel/instagram-comment-activity-deleter.js
Last active
May 27, 2025 19:30
-
-
Save braunglasrakete/837a136af79b43f1de8af41170849601 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. Also works for story replies. For Likes, see my other project.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Instagram Comment Auto-Deletion Script | |
* original by sbolel, updated to work with current page layout as of April 2025 | |
* keep batch size low to reduce risk of Instagram throwing the SOMETHING WENT WRONG error | |
* it waits a little before selecting comments on reload to not throw it out of loop | |
* it's still not perfect, it occasionally throws SOMETHING WENT WRONG, but better semi-automated than not working at all | |
* if you're getting the message that there are no comments to show and you didn't comment on anything, | |
* go back to Home, and comment on something, then come back | |
* | |
* important: UI language must be set to English for the script to work | |
* | |
* 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. | |
* | |
*/ | |
;(async function () { | |
const DELETION_BATCH_SIZE = 12 | |
const DELAY_BETWEEN_ACTIONS_MS = 1500 | |
const DELAY_BETWEEN_CHECKBOX_CLICKS_MS = 300 | |
const DELAY_AFTER_SELECT_CLICK_MS = 8000 // NEW: Delay after clicking "Select" | |
const DELAY_AFTER_COMMENTS_VISIBLE_MS = 2000 // Wait before starting selection | |
const MAX_RETRIES = 60 | |
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) | |
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`) | |
} | |
const clickElement = async (element) => { | |
if (!element) throw new Error('Element not found') | |
element.scrollIntoView({ behavior: 'smooth', block: 'center' }) | |
element.click() | |
} | |
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') | |
} | |
const deleteSelectedComments = async () => { | |
try { | |
const deleteButton = [...document.querySelectorAll('span')] | |
.find(el => ['Delete', 'Löschen'].includes(el.textContent.trim())) | |
if (!deleteButton) throw new Error('Delete button not found') | |
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) | |
} | |
} | |
const scrollAndWaitForMoreComments = async (previousCount) => { | |
window.scrollTo(0, document.body.scrollHeight) | |
for (let i = 0; i < 10; i++) { | |
await delay(1000) | |
const currentCount = document.querySelectorAll('[aria-label="Toggle checkbox"]').length | |
if (currentCount > previousCount) return true | |
} | |
return false | |
} | |
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_AFTER_SELECT_CLICK_MS) // NEW: Wait for comments to load | |
let checkboxes = document.querySelectorAll('[aria-label="Toggle checkbox"]') | |
if (checkboxes.length === 0) { | |
const gotMore = await scrollAndWaitForMoreComments(0) | |
if (!gotMore) { | |
console.log('🚫 No more comments to delete.') | |
break | |
} | |
continue | |
} | |
await delay(DELAY_AFTER_COMMENTS_VISIBLE_MS) // Wait before starting selection | |
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 script | |
try { | |
await deleteActivity() | |
console.log('✅ All comments deleted or none left to delete.') | |
} catch (error) { | |
console.error('Fatal error:', error.message) | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great job! Very useful