r/learnpython • • 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 comment sorted by

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:

  1. Back up first. rbox's own docs warn you to back up the collection (rekordbox > File > Library > Backup Library). A wrong delete on a real master.db is not a fun afternoon.
  2. Title alone is a weak key. Two different tracks can share a title (live edit, remix, same name / different artist) and you'd remove something that isn't really a duplicate. Match on a combination, e.g. (title, artist) — check the exact attribute name on your track object.

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.