Scott Hanselman

Removing Dead Tracks (Duplicates that don't exist) from iTunes using C#

July 04, 2010 Comment on this post [36] Posted in Source Code | Tools
Sponsored By

image I hate iTunes on Windows with the heat of a thousand suns. It is a pox on my existence and it has cost me hours of pain and suffering. I'm sure it's all unicorns and spun sugar on MAC, but as a guy using Windows with dozens of gigabytes of music on a shared file server that I've ripped since the first CD I ever purchased, it's hell on earth. I could manage all my music with Zune (and I do have a Zune Pass for leasing music I don't want to buy) but there are iDevices in the house and iTunes is what it takes.

Did I mention it sucks? It's slow, frustrating, and is effectively just a giant listbox that exists only to mess up my music library and occasionally try to sneak Safari and QuickTime onto my machines. But enough about Satan, let's yank some dead tracks, shall we?

At some point, I ended up with hundreds of dead tracks and/or duplicates. I ended up with "LL Cool J - I Need Love 1.mp3" and "LL Cool J - I Need Love 2.mp3" along side the original. It was taking up many gigs of duplicate space. I searched for *1.mp3 and *2.mp3, etc, and deleted the dupes on disk.

However, this then left me with a big iTunes database that THINKS it has music, even though the file on disk is long gone. Now, as a napalm-style solution, you CAN delete your iTunes library completely and re-add it. It'll add just files that exist, but you will lose any edits, artwork, etc, you may have added. This solution wasn't cool for me so I say, nay nay.

There's a MILLION stupid little shareware apps that purport to fix duplicates and remove dead tracks. In my case, removing dupes was easy, but yanking dead tracks isn't worth me spending money.

Instead, I sat down tonight and decided to write a script in C# that said something like "foreach track, does that track exist where the system thinks it should be? No? Delete it."

A little googling with Bing, however, brought me to a three year old post at ScarTech where Shawn (shame he's stopped blogging) did the code for me! Yay. Here's a bit of his code, using the iTunes COM SDK. His stuff was written using Visual Studio Express 2008, but I upgraded to 2010. Also, I'm on 64-bit and his COM code expects x86, so I changed the project properties from AnyCPU to x86 and it worked great.

//create a reference to iTunes
iTunesAppClass iTunes = new iTunesAppClass();
//get a reference to the collection of all tracks
IITTrackCollection tracks = iTunes.LibraryPlaylist.Tracks;
for (int i = trackCount; i > 0; i–)
IITTrack track = tracks[i];
if (track.Kind == ITTrackKind.ITTrackKindFile)
IITFileOrCDTrack fileTrack = (IITFileOrCDTrack)track;
//if the file doesn’t exist, we’ll delete it from iTunes
if (fileTrack.Location == String.Empty || !System.IO.File.Exists(fileTrack.Location))

Here's before:


So I ran it...

iTunes COM Sample Screenshot 

...and here's the afterparty:


Looks like it checked 7219 tracks and removed 570 dead ones. Yum.

I'd also encourage you to check out the second part of Shawn's iTunes and C# tutorial series where he creates a duplicate finder that knows to keep the higher bitrate song.

You can download Shawn's code here but as he hasn't blogged in a LONG time, I've mirrored it at SkyDrive. Big thanks to Shawn at ScarTech for saving me my evening and for tidying up my iTunes.

Good stuff, the internets.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
July 04, 2010 6:04
If you happen to be on the Unicorns and spun sugar platform (let me dispel that myth right away btw, it's still feels like I'm managing my music library via Excel), Super Remove Dead Tracks ( is an AppleScript script that will do this also and works pretty well.
July 04, 2010 7:39
Google "ITLU" (itunes library updater). A free dotnet app that's apparently no longer updated, but still works fine.
July 04, 2010 7:40
LOLed at "A little googling with Bing" :D
July 04, 2010 7:41
Have you tried media monkey instead of iTunes for handling your music. I think it is much better than iTunes, but at the same time you get two application you need to run for synchronizing you iDevice instead of one. Media Monkey doesn't support iOS4 as of the moment, but will probably be realesed shortly. The free version will cover most needs, but there is a gold version with some extra features.
July 04, 2010 7:44
The "Find Dead Tracks" was great, but the "Find duplicates" was very aggressive. The problem with duplicate songs is that you can have the same song on two or more albums (eg I have "Sultans of Swing" on Money For Nothing, Sultans of Swing and Live at the BBC). Why don't you remove your dead tracks and then spend an hour on the weekend cleaning up iTunes?
July 04, 2010 8:24
It's just as bad on MacOS. It can't decide whether it wants to be a music player or a sync hub for media and every type of Apple device known to man, which means it's nearly the same resource hog. It also hijacks the media keys, meaning I've actually renamed and changed the permissions on it so it can't be launched just so I can use Play/Pause with Spotify. This script is great - I ended up doing mine on Windows by hand ...
July 04, 2010 9:47
iTunes is evil on every platform. It's just a bit faster and more stable on OS X.
July 04, 2010 11:27
What about remapping track that are in a removable drive after the drive letters changed?
July 04, 2010 11:53
I'm actually a little surprised that there's not some powershell modules written for iTunes.
July 04, 2010 12:04
That's cute. You hate overambitious software that doesn't work the way you do. "It'll add just files that exist, but you will lose any edits, artwork, etc, you may have added. This solution wasn't cool for me" Oh what's that? You perform edits that you don't want to lose with this software you pretend to hate with the power of sums? That's adorable. It's always fun seeing someone discover platform lock-in for the first time. All that talk about Zune is transparently fake (and invalidates your valid whinges, topping it with fanboism), maintain your music collection properly with metadata in the file instead of in proprietary databases you don't hit these messes.
July 05, 2010 2:07
+1 for MediaMonkey. Talks to iDevices, and is infinitely extendable with an active community writing scripts to do all kinds of things. Still have to use iTunes for updating my iPhone OS etc., but for music management I've never looked back.
July 05, 2010 6:42
JD - Wasn't trying to be fake or fanboish, transparently or otherwise. I just try to use what works...I think over 8 year of blog posts show a history of pragmatism, but your points are valid with regard to metadata. No proprietary database should be needed. What program do you recommend for media management that will store everything (including artwork) in the file's metadata? Christian - I haven't done the math, but I feel like over 6000 tracks would take over and hour to deal with. That's 100 tracks a minute. Maybe it's possible.
July 05, 2010 8:15
Scott - Once again, have a lookt att MediaMonkey. Some of the nice features are organizing the files from their metadata and and autotagging files using amazon where it also looks up the artwork and stores it to the metadata. If you go for gold you'll also have file monitoring for automatic update of your library if someone makes a change to actual file.
July 05, 2010 11:39
Have to agree with the itunes thing, I use it because we have mutiple ipods and iphones in the house "no zunes because now we are on itunes with multiple devices its easier".... or is it. You can't share libraries easily !!! this means my wifes login and mine has a different library and playlists and counts etc, this is poor it is time consuming and it annoys my wife intently. I don't want to turn into a basher but the other area that annoys us intently is the management of tracks, if I import them they don't show up on my wifes list you have to add manually !! NO !! even though you can have these tracks on multiple ipods that are owned by myself they lock it out of the profile presumably as a feature so you can have your own tracklists etc , surely I should be able to choose this...the dupliacte tracks and missing tracks and permission issues is also a symptom of this... Itunes deliberately makes it difficult to do this and I'm stuck with it :(
July 05, 2010 14:39
If you need "tool" to fix the "tool" something is wrong there :)
July 05, 2010 16:25
Ah iTunes, the reason I bought a Nokia E75 instead of an iPhone and gave away an iPod I won. Heat of a thousand suns? pssh!
July 05, 2010 23:59
With "say nay", do I detect a Sir Thomas Wyatt reference? BTW if you have "I need love" I certainly hope you also have "I'm that type of guy" - perhaps the best LL Cool J effort known to date.
July 06, 2010 1:33
iTunes decimated my music's tags to the point that it couldn't even play a large percentage of the tracks. So, I was forced to seek out a manual tagging app. I've used MP3Tag and loved it. It's taken a while to go through tens of GBs of music and properly tag them, but the scraping features in MP3Tag that auto-fill the data/thumbnails are fantastic... and the data's stored right in the file, no worries about sketchy iTunes XML files. Then I set my music as read-only before importing the files into iTunes... and I keep my music outside of the "iTunes Music Folder," just to keep it at a safe distance. Now, when that beautiful and elusive day comes that I can leave these iDevices/iTunes in my past, my music will already be properly tagged, and I will finally find joy.
July 06, 2010 3:18
Hi, thanks for the post. this helped me sort out the same problem on my laptop. What i would like know is, Has anyone tried SuperSync? It purportedly does the removal of broken links as well as syncing music across pc and gadgets. I haven't seen anything about the dups though so ymmv.
July 06, 2010 10:36
Totally agree Scott, iTunes is awful. I have used a small app called Tag & Rename for years its been money well spent. -1 JD - are you an Apple Fanboy per chance - I think Scotts reply is a fair answer to your comments.
July 06, 2010 12:15
iTunes is one of the worst pieces of software I've ever used. That's why I use winamp now. Sync's perfectly with my IPod and you can now import your iTunes setup. Try'll never go back!
July 06, 2010 15:10
I've been meaning to get around to this just haven't had the time, thanks
July 06, 2010 16:12
"Also, I'm on 64-bit and his COM code expects x86, so I changed the project properties from AnyCPU to x86 and it worked great." Didn't you mean the other way around (changed x86 to AnyCPU)?
July 06, 2010 16:13
+1 for mp3tag, great little app for cleaning up mismatched info & bulk renaming/tagging. Mediamonkey was my #1 choice, but the artwork fixes were *way* easier on mp3tag. iTunes is just... Odd.
July 06, 2010 17:06
I feel for you, Scott! I loathe iTunes. I have it installed on a virtual now for the sole purpose of purchasing from their store when absolutely necessary. I use Winamp to manage my entire library (on a NAS) and sync with all my devices (those prefixed with an "i" or otherwise). My wife and I can sync with separate play lists, so we can share the same Winamp install or use a separate install on each of our notebooks and get just the music we are each interested in.
July 06, 2010 17:09
I've used LINQ to XML a few times to tweak stuff in the iTunes Library file, including removing duplicates
July 06, 2010 21:30
B. Clay - Nope. Running AnyCPU on x64 makes it runs as 64-bit then the COM calls fail. Needs to be forced to x86.
July 07, 2010 1:19
Scott, I'm glad the code helped. Considering your intro to http handlers helped me big time, we're even! On a side not, my brother and I are going to start posting again on ScarTech. We plan on sharing some more tools and tricks we use to make our lives a little easier.
July 09, 2010 15:55
Once again you've save me hours of work. I had just done the *1, *2 cleanup and was about sit down to remove the orphans from a 19K song library. Thanks for the help. Now I have more time to figure out AppFabric.
July 09, 2010 16:26
iTunes Toolkit (of which I am the author) is a free program that has a function for finding and either repairing or deleting dead tracks. There are options to use either of two strategies to find dead tracks. It can use the iTunes COM interface to iterate through each track--in the same manner as your sample code. Unfortunately, that can be slow for a large library. Alternatively, a faster mode is achieved by parsing the iTunes Library.xml file. This has the additional benefit of telling where the file was located the last time iTunes was able to find it. The program's optional repair function lets you examine the folder the dead track was located in. This is helpful if the file has been renamed, in which case you can navigate to the track and re-associate it with the dead track. For duplicate tracks, I use iTunes' "Display Duplicates" menu item (on the File menu). It's true that it finds a lot of false positives (i.e. different songs with the same title). However, I can easily eyeball the list and put the real duplicates in a separate playlist. Then I use iTunes Toolkit to delete the duplicate tracks from iTunes and from my hard drive. By the way, I like iTunes a lot. The Column Browser (Ctrl+B) makes it very easy to drill down to a particular set of tracks. Unlike some of your commenters, I admire iTunes' tagging capabilities. Mass tagging is a breeze and autocomplete helps insure consistency, which is especially important for composer and artist tags. I also like the ability to join tracks, which is especially useful for ripping the movements of classical pieces. I found your blog while searching for an alternative to VS2010's horrible help system. Many thanks for that. Mike
July 11, 2010 11:58
I feel your pain. However, there is a solution, it's way less manual than mp3tag (and its FREE), creates usefull (and correct!) metadata, sorts out file & directory naming, downloads album covers etc..... MusicBrainz Picard. Drag files into the window, cluster them and lookup. if the lookup doesn't find the album or tracks, you can scan them where it will create and identify the MP3 using an audio fingerprint, where it will then get the correct album and track info. It will even identify the tracks based on country of origin where albums were released in different territories with different track lists. You can then rationalise the track list ( and leave dupes behind ) with simple drag/drop. I've been looking for something to ease the itunes pain, as i've had my tracks nicely organised (or so I thought) on the filesystem, and its been workable for the last 10 years. I've scanned over 500 albums, and so far its only failed to identify 2 tracks. Its pretty damn good, and well worth a donation. Cheers. Rob
July 11, 2010 13:30
iTunes is almost completely unusable on a shared drive. I finally brought the files local to my primary machine that connects to my iPhone, also made that machine the fileserver, and - voila! - it actually works pretty well. The only other issue is retagging large groups of files, and for that, Tag & Rename has been a savior.
July 13, 2010 5:18
OMG i could not agree more. I had the exact same issue and my library is useless now. I havn't been bothered trying to figure out how to remove the thousands of duplicates iTunes created. Your seriously a legend. I'll check this out later
July 19, 2010 0:44
Hey great post... readers can also check out how to get rid of iTunes duplicates
August 25, 2010 5:41
I removed my dead tracks doing pretty much the same thing, but using F# Interactive.
September 26, 2010 12:27
Thanx a lot....nice work:)

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.