How to remove folders after Plex "deleted" media from library?

How can I delete the leftover files and folders after you have deleted the media from within Plex client? For instance, if I download a show and it has a folder with the video file and some text files or whatever inside, and I watch it and delete the show from within Plex - all that stuff is still there, taking up space. Granted, it’s not a lot of space, but it accumulates over time and it makes my file structure messy.

The same way everybody else does… with Windows Explorer.

That is a terrible answer and not one I would think someone that has posted 1,482 times would give - but who knows, maybe you’ve just given 1,482 terrible answers.

Anyways … does anybody have any HELPFUL answers? A batch file that runs on a scheduled task? Some kind of hook that triggers every time the delete button is clicked?

4 Likes

You could try something like this stackoverflow.com/questions/7831286/how-to-delete-empty-folders-using-windows-command-prompt But that will only remove empty folders.
When you remove files from within plex, it only deletes the media file, that means that subtitles and other files are not removed.

So you need to building a batch script that, look into folders to check if there is a media file, if there is a media file do nothing. If not then delete the folder + left over files.

I just use Windows Explorer, to delete files and folders. Instead of using the delete option in plex.

After some help from the StackOverflow community, I have a solution! I have created a batch file that I will schedule to run once a day to clean out unwanted folders and files from my library.

Copy this to an empty batch file:

@echo off
setlocal EnableDelayedExpansion

rem setting folder variables
set "mymov=E:\Movies\My"
set "hermov=E:\Movies\Her"
set "themmov=E:\Movies\Them"

set folders="%mymov%" "%hermov%" "%themmov%"

for %%f in (%folders%) do (
  cd /D %%f
  for /D %%d in (*.*) do (
    set keep=no
    for %%m in (mkv mp4 wtv avi) do if exist %%~fd\*.%%m set keep=yes
        if !keep!==no rd /S /Q "%%~fd"
  )
)

Set your movie folders at the top. You can include spaces in your folders. This will scan any folder UNDER the folders you specify, so if you go to high, it will erase every folder without a video file. For instance, taking the example folders above, if I only set my folder to “E:\Movies” … it should erase the sub-folders “My”, “Her”, and “Them” because those folders don’t directly have a video file in it - essentially deleting my entire library.

**NOTICE: I am in no way liable for the use of this batch file. I would HIGHLY suggest you test it out on test folders/files that is set up like your file structure before you run this batch file on your live media. This WILL delete folders from your computer, so use at your own risk.

To see which folders it will delete, replace this line:
if !keep!==no rd /S /Q "%%~fd"
with this:
if !keep!==no echo "%%~fd"

Nevermind. Responded to wrong question.

@jrossh21 said:
Sorry, these forums stripped out some of the syntax …

Enclose code blocks in triple backticks `
I repaired the post above.

Is this still the best way to do it? To me this seems like something Plex should take care of, at least as on option.

If this is still the best way: where would I create/run/place this batch file? Not a tech person…

@jrossh21 said:
After some help from the StackOverflow community, I have a solution! I have created a batch file that I will schedule to run once a day to clean out unwanted folders and files from my library.

Copy this to an empty batch file:

@echo off
setlocal EnableDelayedExpansion

rem setting folder variables
set "mymov=E:\Movies\My"
set "hermov=E:\Movies\Her"
set "themmov=E:\Movies\Them"

set folders="%mymov%" "%hermov%" "%themmov%"

for %%f in (%folders%) do (
  cd /D %%f
  for /D %%d in (*.*) do (
    set keep=no
    for %%m in (mkv mp4 wtv avi) do if exist %%~fd\*.%%m set keep=yes
        if !keep!==no rd /S /Q "%%~fd"
  )
)

Set your movie folders at the top. You can include spaces in your folders. This will scan any folder UNDER the folders you specify, so if you go to high, it will erase every folder without a video file. For instance, taking the example folders above, if I only set my folder to “E:\Movies” … it should erase the sub-folders “My”, “Her”, and “Them” because those folders don’t directly have a video file in it - essentially deleting my entire library.

**NOTICE: I am in no way liable for the use of this batch file. I would HIGHLY suggest you test it out on test folders/files that is set up like your file structure before you run this batch file on your live media. This WILL delete folders from your computer, so use at your own risk.

BTW I use a Synology NAS.

@remkovdz said:
Is this still the best way to do it? To me this seems like something Plex should take care of, at least as on option.

If this is still the best way: where would I create/run/place this batch file? Not a tech person…

@jrossh21 said:
After some help from the StackOverflow community, I have a solution! I have created a batch file that I will schedule to run once a day to clean out unwanted folders and files from my library.

Copy this to an empty batch file:

@echo off
setlocal EnableDelayedExpansion

rem setting folder variables
set "mymov=E:\Movies\My"
set "hermov=E:\Movies\Her"
set "themmov=E:\Movies\Them"

set folders="%mymov%" "%hermov%" "%themmov%"

for %%f in (%folders%) do (
  cd /D %%f
  for /D %%d in (*.*) do (
    set keep=no
    for %%m in (mkv mp4 wtv avi) do if exist %%~fd\*.%%m set keep=yes
        if !keep!==no rd /S /Q "%%~fd"
  )
)

Set your movie folders at the top. You can include spaces in your folders. This will scan any folder UNDER the folders you specify, so if you go to high, it will erase every folder without a video file. For instance, taking the example folders above, if I only set my folder to “E:\Movies” … it should erase the sub-folders “My”, “Her”, and “Them” because those folders don’t directly have a video file in it - essentially deleting my entire library.

**NOTICE: I am in no way liable for the use of this batch file. I would HIGHLY suggest you test it out on test folders/files that is set up like your file structure before you run this batch file on your live media. This WILL delete folders from your computer, so use at your own risk.

@remkovdz said:
Is this still the best way to do it? To me this seems like something Plex should take care of, at least as on option.

If this is still the best way: where would I create/run/place this batch file? Not a tech person…

@jrossh21 said:
After some help from the StackOverflow community, I have a solution! I have created a batch file that I will schedule to run once a day to clean out unwanted folders and files from my library.

Copy this to an empty batch file:

@echo off
setlocal EnableDelayedExpansion

rem setting folder variables
set "mymov=E:\Movies\My"
set "hermov=E:\Movies\Her"
set "themmov=E:\Movies\Them"

set folders="%mymov%" "%hermov%" "%themmov%"

for %%f in (%folders%) do (
  cd /D %%f
  for /D %%d in (*.*) do (
    set keep=no
    for %%m in (mkv mp4 wtv avi) do if exist %%~fd\*.%%m set keep=yes
        if !keep!==no rd /S /Q "%%~fd"
  )
)

Set your movie folders at the top. You can include spaces in your folders. This will scan any folder UNDER the folders you specify, so if you go to high, it will erase every folder without a video file. For instance, taking the example folders above, if I only set my folder to “E:\Movies” … it should erase the sub-folders “My”, “Her”, and “Them” because those folders don’t directly have a video file in it - essentially deleting my entire library.

**NOTICE: I am in no way liable for the use of this batch file. I would HIGHLY suggest you test it out on test folders/files that is set up like your file structure before you run this batch file on your live media. This WILL delete folders from your computer, so use at your own risk.

@remkovdz said:
Is this still the best way to do it? To me this seems like something Plex should take care of, at least as on option.

If this is still the best way: where would I create/run/place this batch file? Not a tech person…

It is my understanding that current versions of Plex Server are now deleting empty folders if the last video file in them has been deleted from within Plex.

Hello,
I was looking for something like that…
How to delete empty folders and other files after removing the video.

So I made a simple program to deal with it… You can use if you want to:

It will scan a path (like C:\user\Videos) for empty folders and remove them for you.

Plex might be removing folders IF the folder is empty. But c’mon guys, is there ever JUST a movie file in a folder? Sometimes I even find a “sample.mp4” or something in there - so my script doesn’t work 100% of the time. Plex just needs to figure out how to delete the parent folder of the movie file if the movie is deleted in Plex. It really can’t be that tough.

1 Like

Is there a solution yet when I delete a media file with the Plex client that also the parent folder is deleted?

I have done some tests and found out the following. When I delete the movie via my Samsung TV with a Plex client. The video/movie file is always deleted.
When the video/movie file is the only file in his folder this parent folder is deleted.
When this parent folder for the movie has also .txt file or a .srt file the video/movie file is deleted however the .txt or the .srt is not deleted. Nor the parent folder.

I am using Plex server 1.16.3 for QNAP NAS.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.