[GH-ISSUE #132] I can't delete the playlist tracks by position #79

Closed
opened 2026-02-27 19:25:56 +03:00 by kerem · 3 comments
Owner

Originally created by @raulojeda22 on GitHub (Jul 4, 2018).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/132

I want to delete all the tracks of a playlist and I have a code more or less like this:

$playlist=$this->api->getUserPlaylist($userId,$playlistId);
$snapshotId=$playlist->snapshot_id;
while ($snapshotId){
 $tracks = [ 0 ];
 $snapshotId=$this->api->deletePlaylistTracks($playlistId, $tracks, $snapshotId);
}

But it outputs the error: JSON body doesn't conform to specification

I've also tried using:
$tracks= [
'positions' => 0
];

But it outputs the error: Missing or invalid positions

Expected behaviour:
I want to remove the first track of the playlist until $snapshotId becomes false (the playlist is empty)

Originally created by @raulojeda22 on GitHub (Jul 4, 2018). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/132 I want to delete all the tracks of a playlist and I have a code more or less like this: ``` $playlist=$this->api->getUserPlaylist($userId,$playlistId); $snapshotId=$playlist->snapshot_id; while ($snapshotId){ $tracks = [ 0 ]; $snapshotId=$this->api->deletePlaylistTracks($playlistId, $tracks, $snapshotId); } ``` But it outputs the error: JSON body doesn't conform to specification ``` I've also tried using: $tracks= [ 'positions' => 0 ]; ``` But it outputs the error: Missing or invalid positions Expected behaviour: I want to remove the first track of the playlist until $snapshotId becomes false (the playlist is empty)
kerem 2026-02-27 19:25:56 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@jwilsson commented on GitHub (Jul 4, 2018):

Hi!
positions needs to be an array too, so this should work:

$playlist=$this->api->getUserPlaylist($userId,$playlistId);
$snapshotId=$playlist->snapshot_id;
while ($snapshotId){
  $tracks = [
    'positions' => [0]
  ];
   
  $snapshotId=$this->api->deletePlaylistTracks($playlistId, $tracks, $snapshotId);
}

I think though, in this case replacePlaylistTracks might be a better fit. That is, if you only want to clear a playlist. That will be quicker and a $snapshotId will always be returned from deletePlaylistTracks, even when the playlist is empty.

An example using replacePlaylistTracks:

$this->api->replacePlaylistTracks($playlistId, []);

Passing an empty list of tracks will clear the whole playlist in one go.

Hope this helps!

<!-- gh-comment-id:402544261 --> @jwilsson commented on GitHub (Jul 4, 2018): Hi! `positions` needs to be an array too, so this should work: ```php $playlist=$this->api->getUserPlaylist($userId,$playlistId); $snapshotId=$playlist->snapshot_id; while ($snapshotId){ $tracks = [ 'positions' => [0] ]; $snapshotId=$this->api->deletePlaylistTracks($playlistId, $tracks, $snapshotId); } ``` I think though, in this case `replacePlaylistTracks` might be a better fit. That is, if you only want to clear a playlist. That will be quicker and a `$snapshotId` will always be returned from `deletePlaylistTracks`, even when the playlist is empty. An example using `replacePlaylistTracks`: ```php $this->api->replacePlaylistTracks($playlistId, []); ``` Passing an empty list of tracks will clear the whole playlist in one go. Hope this helps!
Author
Owner

@raulojeda22 commented on GitHub (Jul 5, 2018):

Hi Jwilsson! Thanks for the quick reply. In this case replacePlaylistTracks made the job for me, unfortunately using positions as an array also failed with the message:
"Could not remove tracks, please check parameters."

I'm not sure why this happens, so I will leave it to you. When you find the solution you could update the managing-user-playlists.md file in the "Delete tracks from a user's playlist based on positions" section since it doesn't fit the correct answer. I think that more or less would end up like this:

It would change from this:
$trackPositions = [
5,
12,
];
$api->deletePlaylistTracks('USER_ID', 'PLAYLIST_ID', $trackPositions, 'SNAPSHOT_ID');

To more or less this:
$trackPositions = [
'positions' => [5,12]
];
$api->deletePlaylistTracks('PLAYLIST_ID', $trackPositions, 'SNAPSHOT_ID');

I was a bit confused at the beginning because this example didn't matched the definition of the method in SpotifyWebAPI.md: "Or an array with the key "positions" containing integer positions of the tracks to delete."

Just wanted to say, thanks a bunch for your time and your help!

<!-- gh-comment-id:402632460 --> @raulojeda22 commented on GitHub (Jul 5, 2018): Hi Jwilsson! Thanks for the quick reply. In this case replacePlaylistTracks made the job for me, unfortunately using positions as an array also failed with the message: "Could not remove tracks, please check parameters." I'm not sure why this happens, so I will leave it to you. When you find the solution you could update the managing-user-playlists.md file in the "Delete tracks from a user's playlist based on positions" section since it doesn't fit the correct answer. I think that more or less would end up like this: It would change from this: $trackPositions = [ 5, 12, ]; $api->deletePlaylistTracks('USER_ID', 'PLAYLIST_ID', $trackPositions, 'SNAPSHOT_ID'); To more or less this: $trackPositions = [ 'positions' => [5,12] ]; $api->deletePlaylistTracks('PLAYLIST_ID', $trackPositions, 'SNAPSHOT_ID'); I was a bit confused at the beginning because this example didn't matched the definition of the method in SpotifyWebAPI.md: "Or an array with the key "positions" containing integer positions of the tracks to delete." Just wanted to say, thanks a bunch for your time and your help!
Author
Owner

@jwilsson commented on GitHub (Jul 5, 2018):

No worries @raulojeda22, glad I could help!

I'll take another look at the docs.

<!-- gh-comment-id:402677775 --> @jwilsson commented on GitHub (Jul 5, 2018): No worries @raulojeda22, glad I could help! I'll take another look at the docs.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotify-web-api-php#79
No description provided.