Skip to content

Instantly share code, notes, and snippets.

@astamicu
Last active November 13, 2024 00:10
Show Gist options
  • Save astamicu/eb351ce10451f1a51b71a1287d36880f to your computer and use it in GitHub Desktop.
Save astamicu/eb351ce10451f1a51b71a1287d36880f to your computer and use it in GitHub Desktop.
Script to remove all videos from Youtube Watch Later playlist

UPDATED 22.11.2022

It's been two years since the last update, so here's the updated working script as per the comments below.

Thanks to BryanHaley for this.

setInterval(function () {
    video = document.getElementsByTagName('ytd-playlist-video-renderer')[0];

    video.querySelector('#primary button[aria-label="Action menu"]').click();

    var things = document.evaluate(
        '//span[contains(text(),"Remove from")]',
        document,
        null,
        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
        null
    );

    for (var i = 0; i < things.snapshotLength; i++) 
    {
        things.snapshotItem(i).click();
    }
}, 500);

Non-english users will need to change "Action menu" and "Remove from" to what YouTube uses for their localization.

@Someone117
Copy link

I created a similar script that works by just clicking the 3 dots and then on the "Remove from playlist" button. By default, this script removes 100 videos, but that is also adjustable

This should work for all languages, so paste the script into your console and run.

YT might change their UI and you may have different screen resolution, so you may need to adjust the click x and y for each. You may also need to increase the delay if videos don't load fast enough (you can also run it multiple times).

function simulateClick(x, y) {
    // What acually does the clicking
    var event = new MouseEvent('click', {
        view: window,
        bubbles: true,
        cancelable: true,
        clientX: x,
        clientY: y
    });
    document.elementFromPoint(x, y).dispatchEvent(event);
}

function performClicks() {
    let count = 0;

    function clickSequence() {
        // change this number to remove more videos
        if (count >= 100) {
            console.log("Completed 100 click sequences.");
            return;
        }

        // change this when the UI changes, this should be the location of the 3 dots
        simulateClick(1872, 256);
        console.log(`Click sequence ${count + 1} pt 1`);

        setTimeout(function() {
            // change this when the UI changes, this should be the location of the "Remove From Playlist" button
            simulateClick(1784, 333);
            console.log(`Click sequence ${count + 1} pt 2`);

            count++;

            // Wait 100ms, you may want to set this to more time to let the next video load
            setTimeout(clickSequence, 100);
        // Wait 100ms, you may want to set this to more time to let the next video load
        }, 100);
    }

    // Start
    clickSequence();
}

// Execute the click sequences
performClicks();

This code prints your mouse location to the console so you can tailor the script

document.addEventListener('mousemove', function(event) {
    let x = event.clientX;
    let y = event.clientY;
    console.log(`Mouse position: X: ${x}, Y: ${y}`);
});

@BIRTAX38
Copy link

BIRTAX38 commented Oct 14, 2024

Universal version for all languages:

setInterval(function () {
    video = document.getElementsByTagName('ytd-playlist-video-renderer')[0];

    video.querySelector(`#primary button[aria-label="${window.ytcfg.msgs["VIDEO_ACTION_MENU"]}"]`).click();

    var things = document.evaluate(
        `//span[contains(text(),"${document.querySelector('meta[name="title"]')?.content}")]`,
        document,
        null,
        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
        null
    );

    for (var i = 0; i < things.snapshotLength; i++) 
    {
        things.snapshotItem(i).click();
    }
}, 400);

@jstrocel
Copy link

window.ytcfg

This object returns an undefined object for me when I tried it in english.

@nightbasilisk
Copy link

To remove hidden videos. Click the dots in Watch Later playlist and select "Show unavailable videos" then run this version of the script:

setInterval(function () {
    video = document.getElementsByTagName('ytd-playlist-video-renderer')[0];

    video.querySelector('#primary button').click();

    var things = document.evaluate(
        '//span[contains(text(),"Remove from")]',
        document,
        null,
        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
        null
    );

    for (var i = 0; i < things.snapshotLength; i++) 
    {
        things.snapshotItem(i).click();
    }
}, 1000);

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