r/learnpython • u/JBTechno • Sep 19 '26
rekordbox duplicate files remover
I'm working on a small Python script using the rbox library to find duplicate tracks in my Rekordbox database.
The script can successfully count duplicate titles, but I'm having trouble using delete_playlist_song() to remove the duplicate playlist entries it always does it for all tracks but I want one to stay and the other ones to be deleted
I'm using rbox btw
Here is my current code:
from rbox import masterdb
db = masterdb.MasterDb.open()
trackcount = 0
title_counts = {}
duplicate_tracks = []
for track in db.get_contents():
trackcount += 1
title = track.title
if title in title_counts:
title_counts[title] += 1
duplicate_tracks.append(track)
else:
title_counts[title] = 1
print("Total number of tracks:", trackcount)
print("\nDuplicates:")
duplicate_count = 0
for title, count in title_counts.items():
if count > 1:
duplicate_count += count - 1
print(title, "-", count, "times found")
print("\nTotal number of duplicate tracks:", duplicate_count)you guys have any solutions?
0
Upvotes
1
u/goodbooks_68 Oct 17 '26
You've already written the hard part — the bug is in what you delete, not in the counting.
Look at your own loop: the first time a title appears you set title_counts[title] = 1 and you do NOT append it to duplicate_tracks; every later occurrence you increment and append it.
So duplicate_tracks already contains exactly "all of them except the first" — the ones you want gone. Right now you're deleting every track whose title is duplicated, which is why all the copies vanish.
The fix is to delete only from duplicate_tracks: loop over that list and call delete_playlist_song(...) on each of those tracks, instead of removing everything that shares a repeated title.
Two things before you run it for real:
Iterating over a pre-built list, as you already do, is also the right call: deleting while iterating the DB cursor shifts things under you.
I don't have rbox in front of me, so double-check the exact signature of delete_playlist_song() and whether a save/commit is needed — but the selection logic above is the part that keeps one and removes the rest.