[GH-ISSUE #95] Removing local files from playlists #43

Closed
opened 2026-02-27 23:20:33 +03:00 by kerem · 4 comments
Owner

Originally created by @Hoffs on GitHub (May 24, 2016).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/95

So I have been trying to find a way to remove a local file from a playlist but I can't get it to work any way. I can't really find a function which would do what I need. Both remove_tracks functions require one or another type of argument which has to have track URI. But at least by looking at the Web API on Spotify page it says that it can only be done by using snapshot-id and position (https://developer.spotify.com/web-api/local-files-spotify-playlists/),

'It is not currently possible to add local files to playlists using the Web API, but they can be reordered or removed. The latter should be done by specifying the index and snapshot_id, and NOT the URI of the track.'

So can such functionality be implemented in a new release?

Originally created by @Hoffs on GitHub (May 24, 2016). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/95 So I have been trying to find a way to remove a local file from a playlist but I can't get it to work any way. I can't really find a function which would do what I need. Both remove_tracks functions require one or another type of argument which has to have track URI. But at least by looking at the Web API on Spotify page it says that it can only be done by using snapshot-id and position (https://developer.spotify.com/web-api/local-files-spotify-playlists/), 'It is not currently possible to add local files to playlists using the Web API, but they can be reordered or removed. The latter should be done by specifying the index and snapshot_id, and NOT the URI of the track.' So can such functionality be implemented in a new release?
kerem 2026-02-27 23:20:33 +03:00
Author
Owner

@stavlocker commented on GitHub (Jun 5, 2018):

+1

<!-- gh-comment-id:394880478 --> @stavlocker commented on GitHub (Jun 5, 2018): +1
Author
Owner

@stephanebruckert commented on GitHub (Feb 13, 2020):

You should be able to do this using user_playlist_remove_specific_occurrences_of_tracks by passing a snapshot_id and a tracks array that contains the track position to remove https://spotipy.readthedocs.io/en/2.8.0/#spotipy.client.Spotify.user_playlist_remove_specific_occurrences_of_tracks

<!-- gh-comment-id:585490599 --> @stephanebruckert commented on GitHub (Feb 13, 2020): You should be able to do this using `user_playlist_remove_specific_occurrences_of_tracks` by passing a `snapshot_id` and a `tracks` array that contains the track position to remove https://spotipy.readthedocs.io/en/2.8.0/#spotipy.client.Spotify.user_playlist_remove_specific_occurrences_of_tracks
Author
Owner

@RatkoJ commented on GitHub (May 12, 2020):

I ran into the same issue with removing local files. There are 2 issues:

  1. local tracks need to be removed by position and snapshot_id, but WITHOUT specifying uri. As currently written, Spotipy will not accept tracks without uris.
  2. the formatting of the body actually has to be {"snapshot_id": 12asdsd... , "positions":[1,2,3,4]}

I ended up writing my own function for this and it seems to work:

def remove_tracks_by_position(user, playlist_id, tracks, snapshot_id):
    plid = sp._get_id("playlist", playlist_id)
    payload = {"snapshot_id":snapshot_id, "positions":[]}
    for tr in tracks:
        payload["positions"].extend(tr["positions"])
    return sp._delete("users/%s/playlists/%s/tracks" % (user, plid), payload=payload)
<!-- gh-comment-id:627208310 --> @RatkoJ commented on GitHub (May 12, 2020): I ran into the same issue with removing local files. There are 2 issues: 1. local tracks need to be removed by `position` and `snapshot_id`, but WITHOUT specifying `uri`. As currently written, Spotipy will not accept `tracks` without uris. 2. the formatting of the body actually has to be `{"snapshot_id": 12asdsd... , "positions":[1,2,3,4]}` I ended up writing my own function for this and it seems to work: ``` python def remove_tracks_by_position(user, playlist_id, tracks, snapshot_id): plid = sp._get_id("playlist", playlist_id) payload = {"snapshot_id":snapshot_id, "positions":[]} for tr in tracks: payload["positions"].extend(tr["positions"]) return sp._delete("users/%s/playlists/%s/tracks" % (user, plid), payload=payload) ```
Author
Owner

@20kaushik02 commented on GitHub (Aug 1, 2024):

  1. local tracks need to be removed by position and snapshot_id, but WITHOUT specifying uri. As currently written, Spotipy will not accept tracks without uris.
  2. the formatting of the body actually has to be {"snapshot_id": 12asdsd... , "positions":[1,2,3,4]}

Still works as of date, in both spotipy and the API itself. Also, this body formatting works for cloud/non-local files too, not just local files.

Shame that the API documentation is in such a poor condition

<!-- gh-comment-id:2263634801 --> @20kaushik02 commented on GitHub (Aug 1, 2024): > 1. local tracks need to be removed by `position` and `snapshot_id`, but WITHOUT specifying `uri`. As currently written, Spotipy will not accept `tracks` without uris. > 2. the formatting of the body actually has to be `{"snapshot_id": 12asdsd... , "positions":[1,2,3,4]}` Still works as of date, in both spotipy and the API itself. Also, this body formatting works for cloud/non-local files too, not just local files. Shame that the API documentation is in such a poor condition
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/spotipy#43
No description provided.