11 October 2026 · One command a day
How do I delete log files older than 30 days on Linux?
This command finds every file ending in .log under the current directory that has not changed in more than 30 days, and deletes it. Use it when old application logs are slowly filling your disk and you only want to keep the recent ones.
I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.
The command
find . -name "*.log" -mtime +30 -delete
find looks through a directory and every folder below it. The . tells it to start in the current directory, so cd into the right folder first, or replace the dot with a full path such as /var/www/appname/logs. -name "*.log" matches file names ending in .log. The quotes stop your shell expanding the asterisk before find sees it. -mtime +30 matches files last modified more than 30 full days ago. -delete removes each file that matched. If root owns the files, put sudo in front.
What you will see
$ cd /var/www/appname/logs
$ find . -name "*.log" -mtime +30 -print
./app-2026-08-14.log
./app-2026-08-15.log
./worker/queue-2026-08-20.log
$ find . -name "*.log" -mtime +30 -delete
$
When -delete works, it prints nothing. So run the same command with -print in place of -delete first. That gives you the list of files that would be removed, one path per line, relative to where you started. If the list looks right, run it again with -delete. If the prompt comes back with no output, the delete worked. If you see a "Permission denied" line, run it again with sudo.
When to use it
- Your disk is nearly full and you have found what is using the space: one app's log folder has grown to several gigabytes.
- An app writes a new log file every day and does not clear out old ones itself.
- You want a nightly cron job so old logs never build up again.
Watch out for
Order matters. find reads its options from left to right, and -delete only acts on files that passed the tests written before it. If you type find . -delete -name "*.log", it deletes every file and folder under the current directory before it ever checks the name. Always put -delete last. Run pwd first to check where you are, and do a dry run with -print before every new pattern or folder.
Questions people ask
Does it delete rotated logs like app.log.1 or app.log.2.gz?
No. The pattern has to match the whole file name, so *.log skips them. Use -name "*.log*" to include rotated and compressed copies as well.
How do I run it automatically every night?
Open root's crontab with sudo crontab -e and add a line such as 0 3 * * * find /var/www/appname/logs -name "*.log" -mtime +30 -delete. Use a full path, because cron does not start in the folder you would expect.
Clearing out files on your own schedule, by your own rules, is the kind of thing you can only do on a server you control. You can get the same VPS with 20% off at https://asksteves.co.uk/vps. Affiliate link.
Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.
Everything on this blog runs on one Hostinger KVM 8. This link takes 20% off the same box.
Affiliate link.