Powershell script to remove completed downloads

Windows specific questions, problems.
Post Reply
dalboz

Powershell script to remove completed downloads

Post by dalboz »

Just sharing my script to prune completed torrents. I know qBT has this built-in but Sonarr complains if you turn this on, so I use a script. You need to add localhost to bypass authentication.

Code: Select all

# CONFIG

# Torrents removed if Completed this many minutes ago or older
$PruneAfterMins = 15

# URL of your server including http(s) and port of the WebUI
$ServerURL = 'http://localhost:8080'

# SCRIPT
Clear-Host
$OlderThan = Get-Date (Get-Date).AddMinutes($PruneAfterMins * -1)

$Result = Invoke-RestMethod -Uri "$ServerURL/query/torrents?filter=completed"

foreach ($Torrent in $result) {
    # timestamps are in UTC
    $Fin = [timezone]::CurrentTimeZone.ToLocalTime((Get-Date 01.01.1970) + ([System.TimeSpan]::fromseconds($Torrent.completion_on)))
    $Prune = ($Fin -lt $OlderThan)
    Write-Output $Torrent.name
    Write-Output ('Hash = ' + $Torrent.hash)
    Write-Output ('Finished on ' + $Fin + ' [' + $Torrent.completion_on + ']')
    Write-Output "Prune = $Prune"
    Write-Output ""

    If ($Fin) {
        $Body = ('hashes=' + $Torrent.hash)
        Invoke-RestMethod -Method Post -Uri "$ServerURl/command/delete" -Body $Body
    }
    
}
jeremysherriff

Re: Powershell script to remove completed downloads

Post by jeremysherriff »

Thanks, your script gave me a couple of key elements to create my own version in python, for the linux community.

Slightly different in that I don't simply delete based on time, I delete based on the seeding limit being reached and therefore the torrent being in the `pausedUP` state.

FYI: https://qbforums.shiki.hu/index.php/topic,7206.0.html
Post Reply