Skip to content

Instantly share code, notes, and snippets.

@beatwiz
Last active April 22, 2022 12:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beatwiz/58dac7e2b32425ce81417d4a365f2cc9 to your computer and use it in GitHub Desktop.
Save beatwiz/58dac7e2b32425ce81417d4a365f2cc9 to your computer and use it in GitHub Desktop.
[bash] cPanel / WHM Cron bash script to delete all accounts > domains > users, trash / spam / junk emails with more than 30 days
#!/bin/bash
# All users from all domains from all accounts in /home
MAILDIRS=$(find /home/*/mail/ -type d)
# Only .Trash .Junk .Spam folders
INBOXFOLDERS=(.Trash .Junk .spam)
for basedir in $MAILDIRS; do
for ((i = 0; i < ${#INBOXFOLDERS[*]}; i++)); do
# Only cur and new folders
for dir in cur new; do
# Only if the full path to the folder exists
[ -e "$basedir/${INBOXFOLDERS[$i]}/$dir" ] && (
# Delete files with more than 30 days
find "$basedir/${INBOXFOLDERS[$i]}/$dir/" -type f -mtime +30 -delete
)
done
done
done
# Save as /folder/cleartrash.sh and setup on cron daily:
# /folder/cleartrash.sh >/dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment