A download folder fills up over time. This page deletes files older than 30 days from one folder, together with the folders that become empty, once a day. First you look at what would be deleted, then you let it run by itself. The commands were tested on Ubuntu 26.04 LTS; run them as root. In the examples, the folder is /srv/downloads; use your own.

⚠ Deleted is deleted

There is no recycle bin. Use this only on a folder that holds nothing you want to keep, and stop seeding torrents first: when their files are deleted, your torrent client reports them as missing.


1. Look First

find /srv/downloads -mindepth 1 -type f -mtime +30 -print

This only lists the files that were last changed more than 30 days ago. -mindepth 1 keeps the folder itself out of it. Check the list. To keep files longer, change +30, for example to +60.

2. Delete Once by Hand

find /srv/downloads -mindepth 1 -type f -mtime +30 -print -delete
find /srv/downloads -mindepth 1 -type d -empty -print -delete

The first line deletes the old files and prints each one. The second removes folders that are empty afterwards, for example the folder of a series whose episodes were all deleted. A folder with only old episodes is removed; a folder with new episodes stays with all its files.

3. Every Day by Itself

Create the file /etc/systemd/system/clean-downloads.service:

[Unit]
Description=Delete downloads older than 30 days

[Service]
Type=oneshot
ExecStart=/usr/bin/find /srv/downloads -mindepth 1 -type f -mtime +30 -print -delete
ExecStart=/usr/bin/find /srv/downloads -mindepth 1 -type d -empty -print -delete

and the file /etc/systemd/system/clean-downloads.timer:

[Unit]
Description=Daily clean-up of old downloads

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

Switch the timer on and start one run now to check it:

systemctl daemon-reload
systemctl enable --now clean-downloads.timer
systemctl start clean-downloads.service
journalctl -u clean-downloads.service -n 20

The log lists every deleted file and folder. systemctl list-timers clean-downloads.timer shows the next run: every night at midnight, server time. To stop it: systemctl disable --now clean-downloads.timer.

More about timed tasks: Run Tasks on a Schedule on Linux: cron and systemd Timers. Where the space goes: What Fills the Disk and What Loads the Server.


Related Guides

View all Linux guides

Was this answer helpful? 0 Users Found This Useful (0 Votes)