Delete Videos with the playlists and playlistItems Resources

Learn to delete videos by using the playlist and playlistItems resources.

Both the playlists and playlistItems resources offer a DELETE method. The playlistItems resource helps users in removing a specific video from the playlist. On the other hand, the playlists resource allows users to remove the entire playlist from the channel.

Delete individual videos

In this part of the lesson, we’ll delete a specific video from the playlist by sending a DELETE request to the following URL:

https://www.googleapis.com/youtube/v3/playlistItems

Request parameters

The DELETE request only requires one parameter to delete the playlist item, which is the ID of the playlist item that the user wants to delete.

Press + to interact
// Importing libraries here
import fetch from 'node-fetch';
// Define endpoint URL here
const endpointUrl = new URL('https://www.googleapis.com/youtube/v3/playlistItems');
// Define Header Parameters here
const headerParameters = {
Authorization: 'Bearer {{ACCESS_TOKEN}}',
Accept: 'application/json',
};
const queryParameters = new URLSearchParams({
id: '{{PLAYLIST_ITEM_ID}}',
});
// Setting API call options
const options = {
method: 'DELETE',
headers: headerParameters,
};
// Function to make API call
async function deletePlaylistItem() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
// Custom function for printing the API response
printResponse(`${response.status}:${response.statusText}`);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Calling function to make API call
deletePlaylistItem();

Let’s take a look at the following code explanation for the above code:

  • Line 5: We define the endpointUrl variable in which we pass the endpoint URL.

  • Lines 12–14: We define a queryParameters variable in which we pass the playlist item ID we want to delete.

Response

In the case of successful execution of the above code, it returns the response code 204.

Delete a playlist

In this part of the lesson, we’ll delete a specific playlist by sending a DELETE request to the following URL:

https://www.googleapis.com/youtube/v3/playlist

Request parameters

The delete request only requires one parameter to delete the playlist, which is the ID of the playlist that the user wants to delete.

Press + to interact
// Importing libraries here
import fetch from "node-fetch";
// Define endpoint URL here
const endpointUrl = new URL('https://www.googleapis.com/youtube/v3/playlists');
// Define Header Parameters here
const headerParameters = {
Authorization: 'Bearer {{ACCESS_TOKEN}}',
Accept: 'application/json',
};
const queryParameters = new URLSearchParams({
id: '{{PLAYLIST_ID}}',
});
// Setting API call options
const options = {
method: 'DELETE',
headers: headerParameters,
};
// Function to make API call
async function deletePlaylist() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
// Custom function for printing the API response
printResponse(`${response.status}:${response.statusText}`);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Calling function to make API call
deletePlaylist();

Let’s take a look at the following code explanation for the above code:

  • Line 5: We define the endpointUrl variable in which we pass the endpoint URL.

  • Lines 12–14: We define a queryParameters variable in which we pass the playlist item ID that we want to delete.

Response

In the case of successful execution of the above code, it returns the response code 204.