2
votes

Question: How to use delete of playlistItems in Google Apps Script?


What I tried:

  1. All these give Missing name after . operator. (line 123, file "Code") error (I have a sense it might be related to JavaScript delete operator, not sure.):

YouTube.PlaylistItems.delete() YouTube.PlaylistItems.delete("PLi22jkbHFzDjQNWcy4qfLamNjyb0nvkq8") YouTube.PlaylistItems.delete({id: "PLi22jkbHFzDjQNWcy4qfLamNjyb0nvkq8"})

  1. Apparently, executes fine, but no effect:
  var payload =
      {
        "id" : "PLi22jkbHFzDjQNWcy4qfLamNjyb0nvkq8",
      };

  var options =
      {
        "method" : "delete",
        "payload" : payload
      };

  Logger.log(UrlFetchApp.fetch("https://www.googleapis.com/youtube/v3/playlistItems", options));

Any help would be greatly appreciated.


Extra details. The thing I want to do is clear all of the items in a playlist and I already have the following code:

  var result = YouTube.PlaylistItems.list('id', {
    playlistId: "2L1YG9ktx9sVdo-PMFD2iwCC-UWmkYrgQ-"
  });

  Logger.log(result.items.length);

  var items = result.items;

  for (var i = 0; i < items.length; i++) {
    Logger.log(items[i].id);
    // Deletion of the item with id of "items[i].id is expected to happen here
  }
2

2 Answers

1
votes

You don't use the videoId, you use the id of the playlistItem...

while (nextPageToken != null) {

  var playlistResponse = YouTube.PlaylistItems.list('snippet', {playlistId: playlistId, maxResults: 50,pageToken: nextPageToken});

  for (var j = 0; j < playlistResponse.items.length; j++) {
    var playlistItem = playlistResponse.items[j];
    playlistItemsDelete(playlistItem.id)
    //Logger.log('[%s] Title: %s', playlistItem.snippet.resourceId.videoId,  playlistItem.snippet.title);
  }

...And then...

function playlistItemsDelete(id) { //var params = {'onBehalfOfContentOwner': 'everythingability'}; // See full sample for function

YouTube.PlaylistItems.remove(id)

}

1
votes

I think it is not delete() in Apps script. Try YouTube.PlaylistItems.remove(id) instead. I tried adding this method in my script and it automatically populated remove method in the dropdown. There is no delete method.

Hope that helps!