Forked from braunglasrakete/Instagram Comment Activity Deleter
Created
April 24, 2025 02:58
-
-
Save alessandroamella/6602d897efda6d9fc1770a6a231450d3 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 | |
* improved with better scrolling functionality for comment containers | |
* | |
* 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 // Delay after clicking "Select" | |
const DELAY_AFTER_COMMENTS_VISIBLE_MS = 2000 // Wait before starting selection | |
const MAX_RETRIES = 60 | |
const SCROLL_ATTEMPTS = 5 // Number of scroll attempts to load more comments | |
const SCROLL_DELAY_MS = 1000 // Delay between scroll attempts | |
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 findCommentsContainer = () => { | |
// Look for likely comment containers | |
const possibleContainers = [ | |
// Main container with overflow properties | |
document.querySelector('div[style*="overflow"]'), | |
// Container with many child elements | |
[...document.querySelectorAll('div')].find(div => | |
div.querySelectorAll('[aria-label="Toggle checkbox"]').length > 2), | |
// Fallback to document body if specific container can't be found | |
document.body | |
] | |
return possibleContainers.find(container => container !== null) | |
} | |
const scrollAndWaitForMoreComments = async (previousCount) => { | |
const container = findCommentsContainer() | |
console.log('Found container for scrolling:', container) | |
let moreCommentsLoaded = false | |
for (let i = 0; i < SCROLL_ATTEMPTS; i++) { | |
// Scroll both the container and the window to be safe | |
if (container) { | |
container.scrollTop = container.scrollHeight | |
} | |
window.scrollTo(0, document.body.scrollHeight) | |
await delay(SCROLL_DELAY_MS) | |
const currentCount = document.querySelectorAll('[aria-label="Toggle checkbox"]').length | |
console.log(`Scroll attempt ${i+1}/${SCROLL_ATTEMPTS}: ${previousCount} → ${currentCount} comments`) | |
if (currentCount > previousCount) { | |
moreCommentsLoaded = true | |
break | |
} | |
} | |
return moreCommentsLoaded | |
} | |
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) // Wait for comments to load | |
let checkboxes = document.querySelectorAll('[aria-label="Toggle checkbox"]') | |
if (checkboxes.length === 0) { | |
console.log('No checkboxes found. Attempting to scroll to load more comments...') | |
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 | |
const checkboxesBeforeScroll = checkboxes.length | |
if (checkboxesBeforeScroll < DELETION_BATCH_SIZE) { | |
console.log(`Only ${checkboxesBeforeScroll} comments visible. Attempting to load more...`) | |
await scrollAndWaitForMoreComments(checkboxesBeforeScroll) | |
checkboxes = document.querySelectorAll('[aria-label="Toggle checkbox"]') | |
console.log(`After scrolling: ${checkboxes.length} comments available`) | |
} | |
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 | |
console.log('🚀 Starting Instagram Comment Auto-Deletion 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