Add "on delete" trigger to Connections

This is a copy paste of the issue I created on github originally, so I’m copying it here as I cannot seem to find a definitive answer on where Feature requests go. Close whichever you see fit

Describe the problem
My overarching problem is that I’d like to keep only n amount of episodes on the disk (both sonarr and download client). TO be more precise, I only want the last episodes of shows like The Daily show. From what I’ve seen, this was a requested feature for quite some time (#314 and all its duplicates) however no progress has been made on this for at least 7 years from what I can tell.

Currently I have to manually delete the files first in the sonarr directory (either through plex, manually or from sonarr itself) and then manually in the torrent client, which can be quite time consuming.

Describe any solutions you think might work
What I have been told only now is that Plex can handle this deletion behaviour on its own through the advanced settings. When the episode is deleted by Plex, Sonarr will pick this up and–if it is set to do so–unmonitor these episodes.
These events are visible in the history.

What I propose is to add an “on delete” to the “Connection settings”.

This way, anyone could make their own script to tell their download client to delete the episodes as well, seeing as the hash is passed to the scripts/webhooks.

Another option would be to build this option straight into sonarr by adding an advanced option to “Delete torrent data from client when episode is removed and unmonitored.” Either this could be for all shows, or a toggle for specific shows.

Honestly, even the option to “Remove data from torrent client” either by adding another column with checkboxes to this menu or just one single checkbox for all the ones that are being processed would already help speed my use case up. And I am fairly sure this would apply to a lot of people who are keeping up to date with any daily shows.

Please help us automatically delete episodes in one way or another :frowning:


same demand here, I’d like an option built in Sonarr to automatically delete episodes after they are viewed in Plex. The option that already exists in Plex to do that is not good enough because it doesn’t impact what happens in Sonarr afterwards (un-monitoring episodes for exemple). If Sonarr can be in charge of this, everything would be cleaner.

So in addition to your “on delete” option, I propose a “on viewed” option as well.


Pretty sure this is sonarr monitoring a file system, not plex directly :slight_smile:

Im afraid this would never happen. You are asking sonarr to not only keep track of its own system but now keep track of plex too? The webhooks are shit in plex and very unreliable for a system to actually delete files. I would never do this in sonarr as what happens in plex is not related to sonarr in any way.

You should handle this with plex webhooks and hope for the best that it gets deleted when it should. You can not rely on media.stop from plex because it only sends by physically stopping it. When an episode plays through to the end there is no media.stop to trigger a check if it was played all the way.

You would literally have to process every episode every time a webhook triggers to determine what has been played fully vs what has been a partial play (if you want to make sure no accidental deletes are done). This simply isn’t realistic beyond a “it would be nice if…”

Thx for your reply.

Don’t you think it would be possible to link Plex and Sonarr if the guys from Plex could add the right option? I could ask them also.

There is likely a webhook when things are deleted in plex which is as far as they would go. Would be up to you to grab that information and use it to achieve what you are wanting

you view it in plex so the hooks would need to be in there.

if they exist then when a view event is triggered you would need to run a script that would need to work out the series, season, and episode, that file belongs to in sonarr and then un-monitor it (or whatever you wanted to do) via the sonarr (v3) api

how is sonarr supposed to know when youve watched something in plex, or anything else for that matter?

anything wrong with going to the activity view and clicking on the X at the end of the ones you no longer want?

if as has been mentioned you cant trust plex hooks then you may need to write your own scheduled script

  • write a script to scan through every series in sonarr (via the api) at a frequency that works for you
  • have the script check if there is more than one episode in the series
  • if there is then it will delete (and un-monitor) all but the last one

you still have to manually clean up any currently downloading torrents but it might be possible to pull the grab history of the episode via the sonarr api as well, in which case your script should be able to use your download clients api to check if any are active and kill them.

even if sonarr had an event for manual deletes, as well as deletes found while scanning, you would have to ask them to make sure they also include any active torrent hashes so that you can clean those up.

its pretty much an edge use case, how many people would find that feature useful? also, dont forget that sonarr is an automated download system, its not a media management system.

So basically an “on download” script in sonarr. Check the series name/id of the episode that was downloaded, query sonarr’s api to get the number of episodes for that series on disk, delete oldest episodes if they go over whatever limit you want…
Very tongue-in-cheek comment, don’t take this seriously, but: I’ve seen lots of people ask this (for years, as you say) but nobody has ever put in a little work to script this. So maybe people can live with it after all? :wink:

It would be quite simple to build a script to do this. The issue would be you would have to build a complete UI wrapper around sonarr’s DB to be able to have a UI for picking which shows, setting how many episodes, etc.

Something that would take way longer to re-invent the wheel can be done in sonarr quite easily (compared to the alternative)

As you mentioned though a small percentage want this so it isn’t a priority. The next question is if someone took the time to build it into sonarr, would they even want to merge it into the system? If not then you would for sure need to build a simple UI to setup/maintain these external features. A sonarr extension of random non-core features so to speak.

Why’d you need a separate UI to pick series?
Tag the series with “keep10” or whatever, make a custom connection for your script with the same tag and you’re done.

If you like, make a generic one that accepts a parameter for how many to keep, and make as many wrapper scripts as you like that just call your generic script with the parameter filled in.
Create as many custom connections with a meaningful tag as you like in sonarr, all linking to one of the distinct wrapper scripts.

Yea, good point. I guess tag manipulation would work as well :slight_smile:

Not even manipulation, you’d just want a different script called if you wanted different values, you’re just using the tag to control which custom script gets called.

Ask and ye shall receive:

(post edited to remove code)

Oh damn, I just came back to this and this looks really sexy. Thanks for this! Do you think you could throw this onto GitHub or something? I’d love to test this and expand it a little further, alongside with making sure this applies only to series that have a specific tag. (looks like that’s already taken care of in sonarr)

Thrown on github, as requested :smile:

Amazing, cheers. I’ll be making some PRs into it, feel free to chat me up on Discord if you’re up to it!

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