Skip to content

Instantly share code, notes, and snippets.

@pawelszydlo
Last active May 21, 2023 23:51
  • Star 55 You must be signed in to star a gist
  • Fork 22 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save pawelszydlo/e2e1fc424f2c9d306f3a to your computer and use it in GitHub Desktop.
Script to clear finished torrents from transmission-daemon
#!/bin/bash
# Clears finished downloads from Transmission.
# Version: 1.1
#
# Newest version can always be found at:
# https://gist.github.com/pawelszydlo/e2e1fc424f2c9d306f3a
#
# Server string is resolved in this order:
# 1. TRANSMISSION_SERVER environment variable
# 2. Parameters passed to this script
# 3. Hardcoded string in this script (see below).
# Server string: "host:port --auth username:password"
SERVER="host:port --auth user:pass"
# Which torrent states should be removed at 100% progress.
DONE_STATES=("Seeding" "Stopped" "Finished" "Idle")
# Get the final server string to use.
if [[ -n "$TRANSMISSION_SERVER" ]]; then
echo -n "Using server string from the environment: "
SERVER="$TRANSMISSION_SERVER"
elif [[ "$#" -gt 0 ]]; then
echo -n "Using server string passed through parameters: "
SERVER="$*"
else
echo -n "Using hardcoded server string: "
fi
echo "${SERVER: : 10}(...)" # Truncate to not print auth.
# Use transmission-remote to get the torrent list from transmission-remote.
TORRENT_LIST=$(transmission-remote $SERVER --list | sed -e '1d' -e '$d' | awk '{print $1}' | sed -e 's/[^0-9]*//g')
# Iterate through the torrents.
for TORRENT_ID in $TORRENT_LIST
do
INFO=$(transmission-remote $SERVER --torrent "$TORRENT_ID" --info)
echo -e "Processing #$TORRENT_ID: \"$(echo "$INFO" | sed -n 's/.*Name: \(.*\)/\1/p')\"..."
# To see the full torrent info, uncomment the following line.
# echo "$INFO"
PROGRESS=$(echo "$INFO" | sed -n 's/.*Percent Done: \(.*\)%.*/\1/p')
STATE=$(echo "$INFO" | sed -n 's/.*State: \(.*\)/\1/p')
# If the torrent is 100% done and the state is one of the done states.
if [[ "$PROGRESS" == "100" ]] && [[ "${DONE_STATES[@]}" =~ "$STATE" ]]; then
echo "Torrent #$TORRENT_ID is done. Removing torrent from list."
transmission-remote $SERVER --torrent "$TORRENT_ID" --remove
else
echo "Torrent #$TORRENT_ID is $PROGRESS% done with state \"$STATE\". Ignoring."
fi
done
@pawelszydlo
Copy link
Author

Does this script delete only the finished torrents or it deletes the downloaded data also?

It only removes the transmission download. It does not touch the files.

@FredrikM97
Copy link

Does this script delete only the finished torrents or it deletes the downloaded data also?

If you modify "transmission-remote $SERVER --torrent "$TORRENT_ID" --remove" you can change so it also removes the files :)

@treecrazy
Copy link

treecrazy commented Oct 25, 2020

Running this script manually from the terminal, I get an error:

transmission_remove_finished.sh: 18: transmission_remove_finished.sh: Syntax error: "(" unexpected<

Any idea what I may be doing wrong?

@jcrincon
Copy link

jcrincon commented Nov 21, 2020

#!/bin/bash
HAUGENE_ID=`/usr/bin/docker ps | grep "haugene" | awk '{print $1}'`
docker exec -e tmpUser=$RPC_U -e tmpPass=$RPC_P $HAUGENE_ID /shared/remove_finished

Most likely it was:

/shared/remove_finished_torrents

The script also applies to Transmission (vanilla):

#!/bin/bash
TRANSMISSION_ID=`/usr/bin/docker ps | grep "transmission" | awk '{print $1}'`
docker exec -e tmpUser=$RPC_U -e tmpPass=$RPC_P $TRANSMISSION_ID /shared/remove_finished_torrents

PS. For me it was crontab -e

@jmrushing
Copy link

Most likely it was:

/shared/remove_finished_torrents

Good catch. This was a typo. I just corrected it in the original comment.

The script also applies to Transmission (vanilla):

Glad it works for you. I've had this script running on my setup for roughly 2 years without issue.

PS. For me it was crontab -e

Ah yes, I copied the terminal output after the fact (crontab -l) rather than document how to actually edit it. 🙃

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