2 YouTube Data API traps I hit deleting unavailable videos from playlists
Open an old YouTube playlist and you will often see a line near the top: "Unavailable videos are hidden". Behind it sit every video in the playlist that has since been made private or deleted. YouTube keeps the entries,
Open an old YouTube playlist and you will often see a line near the top: "Unavailable videos are hidden". Behind it sit every video in the playlist that has since been made private or deleted. YouTube keeps the entries, hides them by default, and gives you no way to clear them except one at a time.
I built TidyList, a web app for cleaning up big YouTube playlists, on the YouTube Data API v3, and clearing these entries was one of the first things it had to do. Here is what they actually are to the API, the two traps I hit finding them, and how to remove them.
The manual way
- Open the playlist on YouTube, open its menu and choose Show unavailable videos.
- Scroll the whole playlist looking for the greyed-out Private video and Deleted video entries.
- For each one, open its menu and choose Remove from playlist.
Fine for three entries. On a playlist of a few hundred, it is a long scroll, a lot of clicks, and nothing tells you when you have found them all.
What the API returns for them
playlistItems.list returns unavailable entries like any other, with no flag saying "unavailable". What changes is the snippet:
-
snippet.titleis the literal string"Private video"or"Deleted video". -
snippet.thumbnailsis empty. -
snippet.videoOwnerChannelTitleandvideoOwnerChannelIdare gone.
What survives is more useful than you might expect:
-
contentDetails.videoId, so you can still search for what the video was. -
snippet.publishedAt, which on a playlist item is the date it was added to the playlist, not the video's upload date. - The entry's own
id, which is all you need to delete it.
Trap 1: don't trust the title alone
A public video can be titled "Private video". Match on the title only and you will flag it, and maybe delete it. YouTube strips the title and the thumbnail together, so require both:
const PRIVATE_VIDEO_TITLE = 'Private video';
const DELETED_VIDEO_TITLE = 'Deleted video';
function isUnavailable(item: PlaylistItem): boolean {
const title = item.snippet?.title;
const hasThumbnail = Object.keys(item.snippet?.thumbnails ?? {}).length > 0;
return (title === PRIVATE_VIDEO_TITLE || title === DELETED_VIDEO_TITLE) && !hasThumbnail;
}
If the two signs ever disagree, treat the entry as ordinary. The worst case is that it stays in the playlist, which is the safe direction.
Trap 2: you can't copy them anywhere
If you are copying or moving videos between playlists, leave these out. playlistItems.insert with a private or deleted video id fails with 400 Precondition check failed. Filter them before you plan the write, not after the first failure.
Removing them
Deleting is the ordinary call, by the playlist item's id (not the video id):
await fetch(`https://www.googleapis.com/youtube/v3/playlistItems?id=${item.id}`, {
method: 'DELETE',
headers: { Authorization: `Bearer ${accessToken}` }
});
Two things to plan for:
- Quota. Reading is cheap, 1 unit per page of 50 entries. Each delete costs 50 units, so clearing 200 entries is 10,000 units, the whole default daily quota of a new project.
- A 404 means it is already gone. Count it as done rather than as a failure, or a retry after a part-way run will report errors for work that succeeded.
The same works on your Liked videos list, with one difference worth telling users: removing an entry from Liked videos unlikes the video.
Or skip the code
If you just want your own playlists cleaned, that is what I built TidyList for. Sign in, pick a playlist, and it counts the private and removed videos straight away, for free. Clearing them is one step with TidyList Pro (Β£4.99, one-off), and nothing changes on YouTube until you confirm.
Questions about the API side are welcome in the comments.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.