Skip to content

Instantly share code, notes, and snippets.

@sbolel
Last active April 7, 2026 07:10
Show Gist options
  • Select an option

  • Save sbolel/a2b2bfde16b3ab185fbc2e2049240abc to your computer and use it in GitHub Desktop.

Select an option

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)
}
})()
@AtomicRobotMan0101
Copy link
Copy Markdown

@GerardUmbert do you see any errors, attempts at activity that stall, console messages or feedback?

We'll need a bit more info than "no luck here".

Sadly we are dealing with an organisation that is highly defensive, deliberately obfuscatory and hostile due to its decline. We can expect them to throw considerable resources at stopping their audience from leaving and these continuous breakages are part of it.

We can expect what may work for one won't work for another, so close observation really counts.

FWIW I'm now seeing them use long CSS elements that are unique to each page (and element, even duplicates are uniquely named) and seem to be unique for each refresh. They are tracking peoples behaviour at a highly granular and targeted manner. It's corrosive.

Expect things to break, as they are deliberately erecting walls to stop people from doing exactly what this project is attempting.

@theshubzworld
Copy link
Copy Markdown

theshubzworld commented Apr 7, 2026

/*
Check this out, works for me
*/


; (async function () {
    // Constants
    const DELETION_BATCH_SIZE = 20
    const DELAY_BETWEEN_ACTIONS_MS = 700
    const DELAY_BETWEEN_CHECKBOX_CLICKS_MS = 100
    const MAX_RETRIES = 60

    /** XPath selectors */
    const XPATH_SELECT_BUTTON = "//span[text()='Select']/.."
    const XPATH_UNLIKE_BUTTON = '//span[text()="Unlike"]/../../..'
    const XPATH_CONFIRM_UNLIKE_BUTTON = '//button[div[text()="Unlike"]]'
    const XPATH_ERROR_MODAL_OK_BUTTON = '//button[div[text()="OK"]]'

    const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

    const waitForElementByXpath = async (xpath, timeout = 30000) => {
        const startTime = Date.now()
        while (Date.now() - startTime < timeout) {
            const element = document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null)?.iterateNext()
            if (element) return element
            await delay(100)
        }
        throw new Error(`Element with selector "${xpath}" not found within ${timeout}ms`)
    }

    const clickElement = async (element) => {
        if (!element) throw new Error('Element not found')
        element.click()
    }

    const waitForSelectButton = async () => {
        for (let i = 0; i < MAX_RETRIES; i++) {
            console.log("Trying to find Select Button")

            const foundSelect = document.evaluate(XPATH_SELECT_BUTTON, document, null, XPathResult.ANY_TYPE, null)?.iterateNext();
            if (foundSelect) return
            await delay(1000)
        }
        throw new Error('Select button not found after maximum retries')
    }

    /**
     * Unlike selected posts
     */
    const unlikeSelectedPosts = async () => {
        try {
            const unlikeButton = await waitForElementByXpath(XPATH_UNLIKE_BUTTON)
            await clickElement(unlikeButton)
            await delay(DELAY_BETWEEN_ACTIONS_MS)

            const confirmButton = await waitForElementByXpath(XPATH_CONFIRM_UNLIKE_BUTTON)
            await clickElement(confirmButton)

        } catch (error) {
            console.error('Error during unlike:', error.message)
        }
    }

    /**
     * Removes all liked posts
     */
    const removeLikes = async () => {
        try {
            while (true) {

                const allSpans = Array.from(document.querySelectorAll('span'));
                const selectButton = allSpans.find(span => span.textContent === 'Select')?.parentElement;

                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) {

                    const rateLimitPopup = document.evaluate(
                        XPATH_ERROR_MODAL_OK_BUTTON,
                        document,
                        null,
                        XPathResult.ANY_TYPE,
                        null
                    )?.iterateNext()

                    if (rateLimitPopup) {
                        console.log('Instagram rate limit hit, clicking OK')

                        await clickElement(rateLimitPopup)
                        await delay(DELAY_BETWEEN_ACTIONS_MS)

                        await waitForSelectButton()
                        await delay(DELAY_BETWEEN_ACTIONS_MS)
                        continue
                    } else {
                        console.log('No more likes to remove')
                        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 unlikeSelectedPosts()

                await delay(DELAY_BETWEEN_ACTIONS_MS)

                await waitForSelectButton()

                await delay(DELAY_BETWEEN_ACTIONS_MS)
            }

        } catch (error) {
            console.error('Error in removeLikes:', error.message)
        }
    }

    try {
        await removeLikes()
        console.log('Like removal completed')
    } catch (error) {
        console.error('Fatal error:', error.message)
    }

})()

@theshubzworld
Copy link
Copy Markdown

theshubzworld commented Apr 7, 2026

/*
Check this out, for unlike
*/


; (async function () {
    // Constants
    const DELETION_BATCH_SIZE = 20
    const DELAY_BETWEEN_ACTIONS_MS = 700
    const DELAY_BETWEEN_CHECKBOX_CLICKS_MS = 100
    const MAX_RETRIES = 60

    /** XPath selectors */
    const XPATH_SELECT_BUTTON = "//span[text()='Select']/.."
    const XPATH_UNLIKE_BUTTON = '//span[text()="Unlike"]/../../..'
    const XPATH_CONFIRM_UNLIKE_BUTTON = '//button[div[text()="Unlike"]]'
    const XPATH_ERROR_MODAL_OK_BUTTON = '//button[div[text()="OK"]]'

    const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

    const waitForElementByXpath = async (xpath, timeout = 30000) => {
        const startTime = Date.now()
        while (Date.now() - startTime < timeout) {
            const element = document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null)?.iterateNext()
            if (element) return element
            await delay(100)
        }
        throw new Error(`Element with selector "${xpath}" not found within ${timeout}ms`)
    }

    const clickElement = async (element) => {
        if (!element) throw new Error('Element not found')
        element.click()
    }

    const waitForSelectButton = async () => {
        for (let i = 0; i < MAX_RETRIES; i++) {
            console.log("Trying to find Select Button")

            const foundSelect = document.evaluate(XPATH_SELECT_BUTTON, document, null, XPathResult.ANY_TYPE, null)?.iterateNext();
            if (foundSelect) return
            await delay(1000)
        }
        throw new Error('Select button not found after maximum retries')
    }

    /**
     * Unlike selected posts
     */
    const unlikeSelectedPosts = async () => {
        try {
            const unlikeButton = await waitForElementByXpath(XPATH_UNLIKE_BUTTON)
            await clickElement(unlikeButton)
            await delay(DELAY_BETWEEN_ACTIONS_MS)

            const confirmButton = await waitForElementByXpath(XPATH_CONFIRM_UNLIKE_BUTTON)
            await clickElement(confirmButton)

        } catch (error) {
            console.error('Error during unlike:', error.message)
        }
    }

    /**
     * Removes all liked posts
     */
    const removeLikes = async () => {
        try {
            while (true) {

                const allSpans = Array.from(document.querySelectorAll('span'));
                const selectButton = allSpans.find(span => span.textContent === 'Select')?.parentElement;

                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) {

                    const rateLimitPopup = document.evaluate(
                        XPATH_ERROR_MODAL_OK_BUTTON,
                        document,
                        null,
                        XPathResult.ANY_TYPE,
                        null
                    )?.iterateNext()

                    if (rateLimitPopup) {
                        console.log('Instagram rate limit hit, clicking OK')

                        await clickElement(rateLimitPopup)
                        await delay(DELAY_BETWEEN_ACTIONS_MS)

                        await waitForSelectButton()
                        await delay(DELAY_BETWEEN_ACTIONS_MS)
                        continue
                    } else {
                        console.log('No more likes to remove')
                        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 unlikeSelectedPosts()

                await delay(DELAY_BETWEEN_ACTIONS_MS)

                await waitForSelectButton()

                await delay(DELAY_BETWEEN_ACTIONS_MS)
            }

        } catch (error) {
            console.error('Error in removeLikes:', error.message)
        }
    }

    try {
        await removeLikes()
        console.log('Like removal completed')
    } catch (error) {
        console.error('Fatal error:', error.message)
    }

})()

@GerardUmbert
Copy link
Copy Markdown

@GerardUmbert do you see any errors, attempts at activity that stall, console messages or feedback?

We'll need a bit more info than "no luck here".

Sadly we are dealing with an organisation that is highly defensive, deliberately obfuscatory and hostile due to its decline. We can expect them to throw considerable resources at stopping their audience from leaving and these continuous breakages are part of it.

We can expect what may work for one won't work for another, so close observation really counts.

FWIW I'm now seeing them use long CSS elements that are unique to each page (and element, even duplicates are uniquely named) and seem to be unique for each refresh. They are tracking peoples behaviour at a highly granular and targeted manner. It's corrosive.

Expect things to break, as they are deliberately erecting walls to stop people from doing exactly what this project is attempting.

yeah, gimme a sec, it was 3am when I was running it.

Basically I copy-pasted the quoted code and got a single message out, let me run it again and paste it...

"No more Select button — all done! Total deleted: 0"

image I am obviously on the right url and I have a couple adblocks and tampermonkey (no running script) in case that would do something against the script...

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