Skip to content

Instantly share code, notes, and snippets.

@arhamqureshi
Last active March 6, 2025 16:43
Show Gist options
  • Save arhamqureshi/137f0082e873d836c7deef5a5d449666 to your computer and use it in GitHub Desktop.
Save arhamqureshi/137f0082e873d836c7deef5a5d449666 to your computer and use it in GitHub Desktop.
Delete original files after Sonarr/Radarr has finished importing them
# The purpose of this script is to remove the original file after
# Sonarr/Radarr has completed importing the file. This script can be
# scheduled to run using crontab or configured in sonarr/radarr
# to run after every import. This is up to you.
# (https://github.com/Sonarr/Sonarr/wiki/Custom-Post-Processing-Scripts)
# Passing arguments has been disabled in Sonarr v3 which is why I have this
# made this script.
# In order for this script to work properly you will need to disable
# "Rename Episodes" under "Episode Naming" in Sonarr/Radarr settings.
# The script works by checking all the imported files to see if the
# 'file to be imported' has already been imported. If it has then it
# will delete the original file.
# It's best to run this script on a schedule using crontab (or something similar)
# Use at your own risk. I am not responsible for any accidents.
import os, errno, shutil
# Full path to the directory where your files are downloaded to
import_from_dir = 'full/path/to/import_from_dir'
# Full path to the folders in which Sonarr/Radarr imports the files to
# Eg. Movies, TV Shows
folders_to_check = [
'full/path/to/Movies',
'full/path/to/TVShows'
]
# Check if the extension of the file is valid
# Feel free to modify the extensions in the list.
def valid_file_type(filename):
valid_extensions = [
'.3gp', '.3g2', '.asf', '.wmv', '.avi', '.divx', '.evo',
'.f4v', '.flv', '.mkv', '.mk3d', '.mp4', '.mpg', '.mpeg',
'.m2p', '.ps', '.ts', '.m2ts', '.mxf', '.ogg', '.mov', '.qt',
'.rmvb', '.vob', '.webm'
]
extension = (os.path.splitext(filename)[1]).lower()
return True if extension in valid_extensions else False
# Get all files from the specified directory (full path)
# Returns a list of tuples eg. (filename, full_file_path)
def get_files(dir_path):
if os.path.isdir(dir_path):
files = []
for r, d, f in os.walk(dir_path):
for filename in f:
if os.path.isfile(os.path.join(r, filename)) and valid_file_type(filename):
files.append((filename, os.path.join(r, filename)))
return files
else:
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), dir_path)
def main():
files_to_import = get_files(import_from_dir)
for folder in folders_to_check:
folder_files = get_files(folder)
for import_file in files_to_import:
for f_file in folder_files:
# If the filenames match it means the file
# has been imported and the original can be deleted.
if (import_file[0].lower() == f_file[0].lower() and os.path.getsize(import_file[1]) == os.path.getsize(f_file[1])):
print(f"Removing imported file: {import_file[0]}")
os.remove(import_file[1])
# Remove the parent directory of the file and it's contents as
# long as the parent directory is not the input directory we are importing from.
# This prevents empty folders being left inside the import directory and makes it cleaner
parent_dir = os.path.abspath(os.path.join(import_file[1], os.pardir))
if os.path.normpath(import_from_dir) != os.path.normpath(parent_dir):
try:
print(f"Removing imported file parent directory: {parent_dir}")
shutil.rmtree(parent_dir)
except OSError as e:
print(f"Error: {parent_dir} : {e.strerror}")
if __name__ == '__main__':
main()
@RedcoatAsher
Copy link

@Atlarwin make sure the script has execute permissions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment