Cron to purge Trash/Spam folder

I modified this bash to auto clean Trash / Spam folder which might take too much space. But it does not seem to work. Can you please check:

#!/usr/bin/bash
#run this so cron job has environment variable
export hestia='/usr/local/hestia'
#can't force everyone to use the same folders, so catch both variants
declare -a dcfolders=( "Trash" "Spam" )

/usr/local/hestia/bin/v-list-users plain | cut -f1 | sort | while read -r vuser ; do
/usr/local/hestia/bin/v-list-mail-domains $vuser plain | cut -f1 | sort | while read -r vdomain ; do
/usr/local/hestia/bin/v-list-mail-accounts $vuser $vdomain plain | cut -f1 | sort | while read -r vaccount ; do
for dcfolder in "${dcfolders[@]}"
do
if /usr/bin/doveadm mailbox list -u [email protected]$vdomain | grep "^${dcfolder}$";
then
echo  "[email protected]$vdomain $dcfolder"
/usr/bin/doveadm -v expunge -u [email protected]$vdomain mailbox "$dcfolder" savedbefore 30d
fi
done
done
done
done

Ok after I remove “/dev/null” ıt works fine. I think this could be implemented to hestia :slight_smile:

Please create a pull request

It might be worth considering this to set this as an optimal system cronjob. Or as an executable function on request

Also would be nice to allow to delete only emails older then 30 days…

2 Likes

But why cannot I get it run via cron you think?. Cron log says:
/usr/local/hestia/bin/v-list-users: line 108: /data/users/usernamehere/user.conf: No such file or directory
Nobody willing to help :slight_smile:

Maybe you could try something like this:

find $dir -name ‘’ ( -path '.Trash’ -o -path ‘.eliminados’ -o -path '.Spam’ -o -path ‘.Papelera’ -o -path '.Junk’ ) -type f -exec rm {} ;

This is actually working on a plesk server of mine.

I have not ported it yet

The full script is gatbage and I should refactor it with awk

#!/bin/bash
df -h | grep dev
#borrado ficheros muy grandes de hace más de 1 semana
tamano0="+20M"
tiempo0="+7"
#borrado ficheros muy grandes de hace más de 1 mes
tamano1="+16M"
tiempo1="+30"
#borrado ficheros muy grandes de hace más de 3 meses
tamano2="+12M"
tiempo2="+90"
#borrado de ficheros grandes de hace más de 6 meses
tamano3="+8M"
tiempo3="+180"
#borrado de ficheros grandes de hace más de 1 año
tamano4="+4M"
tiempo4="+360"
#borrado de ficheros grandes de hace más de 18 meses
tamano5="+2M"
tiempo5="+540"
#borrado de ficheros grandes de hace más de 2 años
tamano6="+1M"
tiempo6="+720"
#borrado de ficheros medianos de hace más de 3 años
tamano7="+500k"
tiempo7="+1050"
tamano8="+300k"
tiempo8="+1400"
tamano9="+200k"
tiempo9="+1750"

dominio=$1
dir="/var/qmail/mailnames/$dominio"

echo “Borrando de $dir …”
echo “Borrando $tiempo9 días y $tamano9”
find $dir -name ‘’ -path '.’ -ctime $tiempo9 -size $tamano9 -exec rm {} ;
echo “Borrando $tiempo8 días y $tamano8”
find $dir -name '
’ -path ‘.’ -ctime $tiempo8 -size $tamano8 -exec rm {} ;
echo “Borrando $tiempo7 días y $tamano7”
find $dir -name ‘’ -path '.’ -ctime $tiempo7 -size $tamano7 -exec rm {} ;
echo “Borrando $tiempo6 días y $tamano6”
find $dir -name '
’ -path ‘.’ -ctime $tiempo6 -size $tamano6 -exec rm {} ;
echo “Borrando $tiempo5 días y $tamano5”
find $dir -name ‘’ -path '.’ -ctime $tiempo5 -size $tamano5 -exec rm {} ;
echo “Borrando $tiempo4 días y $tamano4”
find $dir -name '
’ -path ‘.’ -ctime $tiempo4 -size $tamano4 -exec rm {} ;
echo “Borrando $tiempo3 días y $tamano3”
find $dir -name ‘’ -path '.’ -ctime $tiempo3 -size $tamano3 -exec rm {} ;
echo “Borrando $tiempo2 días y $tamano2”
find $dir -name '
’ -path ‘.’ -ctime $tiempo2 -size $tamano2 -exec rm {} ;
echo “Borrando $tiempo1 días y $tamano1”
find $dir -name ‘’ -path '.’ -ctime $tiempo1 -size $tamano1 -exec rm {} ;
echo “Borrando $tiempo0 días y $tamano0”
find $dir -name '
’ -path ‘.’ -ctime $tiempo0 -size $tamano0 -exec rm {} ;
echo “Purgamos las papeleras”
#purgamos las papeleras (no probado suficientemente)
find $dir -name ‘’ ( -path '.Trash’ -o -path ‘.eliminados’ -o -path '.Spam’ -o -path ‘.Papelera’ -o -path '.Junk’ ) -type f -exec rm {} ;
df -h | grep dev

I use it to delete big messages older than X days.

  • tamaño means size
  • tiempo means time
  • borrando means deleting

Yes, directly deleting files is an other way. I will keep in my mind. Thanks.

You can also do this in dovecot config by adding eg.
autoexpunge=30d
to the namespace config for each mailbox, if you want to do it over the whole server.

WRT your query about the script not working, cron has a different set of environment variables available, to a normal interactive login.
If you’re curious to see then set a cron job to run env and output to a file.

* * * * * /usr/bin/env > ~/cronenv.txt
Then remove it once you’ve got the results.
You’ll probably find you’ll have to add
HESTIA=/usr/local/hestia
to your script, so it can locate those files in the data directory

Thank you @pluto you gave great ideas.