Reddis
Reddis

Reputation: 21

How clear my YouTube playlist via API? (delete all playlistitems, but not delete playlist)

How clear my YouTube playlist via API? Need delete all playlistitems, but not delete playlist. Of course I know - need loop for 1. get playlistinem ID 2. delete it 3. etc But may be is simpler way? Thanks!

Upvotes: 2

Views: 789

Answers (3)

Gwen Æther
Gwen Æther

Reputation: 1

Here's what I came up with for python:

def clear_playlist(playlist_id):
    playlistitem_id = []
    list_request = youtube.playlistItems().list(
        part="snippet",
        maxResults=50,
        playlistId=playlist_id
    )
    list_contents = list_request.execute()
    for item in list_contents.get('items', []):
        playlistitem_id.append(item.get('id'))

    for video_id in playlistitem_id:
        clear_request = youtube.playlistItems().delete(
            id=str(video_id)
        )
        clear_request.execute()

It only deletes 50 playlist items at a time, but that's a limitation of the API. I'm sure someone smarter than me could make it clear the whole playlist at once.

Edit: just make a while loop. lol :3

Upvotes: 0

Mike Miller
Mike Miller

Reputation: 3507

I solved this problem by deleting the playlist, then recreating it. The following is Kotlin code and mYouTube is an authenticated youtube object as per the API: https://developers.google.com/youtube/v3/guides/authentication

// Delete the playlist
mYouTube.playlists().delete({playlistId}).execute()

// Create a new playlist
val playlist = Playlist()

playlist.snippet = PlaylistSnippet()
playlist.snippet.title = {playlist title}
playlist.status = PlaylistStatus()

mYoutube.playlists().insert("snippet,status", playlist).execute()

Upvotes: 0

Dinesh Arora
Dinesh Arora

Reputation: 2255

Its documented in Youtube API. Here's the flow that you should follow to delete the playListItems. Get the list of plaListItems from PlayList.

https://developers.google.com/youtube/v3/docs/playlists/list - use List

After you get a list of items from your playlist use the "DELETE playListItem" https://developers.google.com/youtube/v3/docs/playlistItems/delete

Upvotes: 1

Related Questions