Powershell Duplicate file Cleanup for Plex Camera Uploads

Some time ago, my wife had her phone stolen and we had not setup any sort of backup for the pictures, so a good number of photos and videos of our kids were lost that day. We now both use Google devices, so we have automatic backup to Drive for free, but I didn’t want to rely only on that.

Enter Plex Camera Upload

We use Plex a lot in our house – one of the major features we use is the automatic upload of photos to a library – this library is shared with parents and in-laws and it also provides a simple way to backup photos or videos taken by our phones. Initially I had some issues with the feature since I was storing files on an SMB share mounted to Linux – and when that happened, I found I needed to restart the upload to get things working.

Since then, things have just worked. Even switching to new devices, once the Plex app is installed and configured for upload, it just works. Until it doesn’t. A recent update broke the upload feature.

It has since been fixed, but in the meantime, I went back and disabled\reset the camera upload capability to try to get it working. Needless to say, I have some duplicate files in my Camera Uploads directory. I think there is some validation in the Plex app for previously updated files, but not much. I had racked up about 4000+ duplicate files and these are not small files, so there was several GB of space savings to be reclaimed. If only there were an automated way to do this…

Powershell to the Rescue

The following script will do the following:

Prompt the user for a path to the Camera Uploads directory

Parse all of the image and video files in the directory and group them by file size where each group has more than 1 file

Generate a SHA1 hash of each of the files to guarantee that they are actually duplicates

Delete hyphenated or malformed date files

A few notes:

I found -1, -2 and -3 duplicate files, there could potentially be more in your case, but the script would need to be modified to accommodate for this

Nothing is deleted unless you pass the -delete parameter to the script and confirm

The directory selection window likes to pop up behind the ISE window…making it look like the script is hung – check the log file for activity in your MyDocs

About the Author

Jacob 'Jake' Rutski works for Citrix on the Channel Architecture team. He holds several certifications from many technology vendors; and while all Citrix products are the majority of his focus, he does enjoy all things tech, automation, and storage - including FreeNAS\ZFS - and has been scripting everything in PowerShell lately...instead of VBScript. The content in this blog represent individual views, not those of Citrix