Delete Videos with the playlists and playlistItems Resources
Learn to delete videos by using the playlist and playlistItems resources.
We'll cover the following
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.
// Importing libraries hereimport fetch from 'node-fetch';// Define endpoint URL hereconst endpointUrl = new URL('https://www.googleapis.com/youtube/v3/playlistItems');// Define Header Parameters hereconst headerParameters = {Authorization: 'Bearer {{ACCESS_TOKEN}}',Accept: 'application/json',};const queryParameters = new URLSearchParams({id: '{{PLAYLIST_ITEM_ID}}',});// Setting API call optionsconst options = {method: 'DELETE',headers: headerParameters,};// Function to make API callasync function deletePlaylistItem() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Custom function for printing the API responseprintResponse(`${response.status}:${response.statusText}`);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API calldeletePlaylistItem();
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.
// Importing libraries hereimport fetch from "node-fetch";// Define endpoint URL hereconst endpointUrl = new URL('https://www.googleapis.com/youtube/v3/playlists');// Define Header Parameters hereconst headerParameters = {Authorization: 'Bearer {{ACCESS_TOKEN}}',Accept: 'application/json',};const queryParameters = new URLSearchParams({id: '{{PLAYLIST_ID}}',});// Setting API call optionsconst options = {method: 'DELETE',headers: headerParameters,};// Function to make API callasync function deletePlaylist() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Custom function for printing the API responseprintResponse(`${response.status}:${response.statusText}`);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API calldeletePlaylist();
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
.