# Cron to purge Trash/Spam folder

**URL:** <https://forum.hestiacp.com/t/cron-to-purge-trash-spam-folder/2275>\
**Category:** Community Support\
**Created:** [December 13, 2020, 11:36am UTC](https://forum.hestiacp.com/t/cron-to-purge-trash-spam-folder/2275 "2020-12-13T11:36:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ozgurerdogan](https://forum.hestiacp.com/letter_avatar_proxy/v4/letter/o/97f17d/32.png) [@ozgurerdogan](https://forum.hestiacp.com/u/ozgurerdogan)\
**Post date:** [December 13, 2020, 11:36am UTC](https://forum.hestiacp.com/t/cron-to-purge-trash-spam-folder/2275/1 "2020-12-13T11:36:56Z")

</div>

I modified this [bash](https://forum.vestacp.com/viewtopic.php?t=16341) 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 $vaccount@$vdomain | grep "^${dcfolder}$";
then
echo "$vaccount@$vdomain $dcfolder"
/usr/bin/doveadm -v expunge -u $vaccount@$vdomain mailbox "$dcfolder" savedbefore 30d
fi
done
done
done
done
```

---

<div class="post-metadata">

**Author:** ![ozgurerdogan](https://forum.hestiacp.com/letter_avatar_proxy/v4/letter/o/97f17d/32.png) [@ozgurerdogan](https://forum.hestiacp.com/u/ozgurerdogan)\
**Post date:** [December 13, 2020, 11:47am UTC](https://forum.hestiacp.com/t/cron-to-purge-trash-spam-folder/2275/2 "2020-12-13T11:47:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eris](https://forum.hestiacp.com/user_avatar/forum.hestiacp.com/eris/32/1241_2.png) [@eris](https://forum.hestiacp.com/u/eris)\
**Post date:** [December 13, 2020, 12:09pm UTC](https://forum.hestiacp.com/t/cron-to-purge-trash-spam-folder/2275/3 "2020-12-13T12:09:39Z")

</div>

Please create a pull request

> **[hestiacp/hestiacp](https://github.com/hestiacp/hestiacp/issues)**
>
> Hestia Control Panel | A lightweight and powerful control panel for the modern web. - hestiacp/hestiacp

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…

---

<div class="post-metadata">

**Author:** ![ozgurerdogan](https://forum.hestiacp.com/letter_avatar_proxy/v4/letter/o/97f17d/32.png) [@ozgurerdogan](https://forum.hestiacp.com/u/ozgurerdogan)\
**Post date:** [December 13, 2020, 1:24pm UTC](https://forum.hestiacp.com/t/cron-to-purge-trash-spam-folder/2275/4 "2020-12-13T13:24:15Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![jlguerrero](https://forum.hestiacp.com/user_avatar/forum.hestiacp.com/jlguerrero/32/954_2.png) [@jlguerrero](https://forum.hestiacp.com/u/jlguerrero)\
**Post date:** [December 13, 2020, 4:18pm UTC](https://forum.hestiacp.com/t/cron-to-purge-trash-spam-folder/2275/5 "2020-12-13T16:18:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ozgurerdogan](https://forum.hestiacp.com/letter_avatar_proxy/v4/letter/o/97f17d/32.png) [@ozgurerdogan](https://forum.hestiacp.com/u/ozgurerdogan)\
**Post date:** [December 13, 2020, 5:19pm UTC](https://forum.hestiacp.com/t/cron-to-purge-trash-spam-folder/2275/6 "2020-12-13T17:19:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pluto](https://forum.hestiacp.com/letter_avatar_proxy/v4/letter/p/f08c70/32.png) [@pluto](https://forum.hestiacp.com/u/pluto)\
**Post date:** [December 15, 2020, 7:28am UTC](https://forum.hestiacp.com/t/cron-to-purge-trash-spam-folder/2275/7 "2020-12-15T07:28:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pluto](https://forum.hestiacp.com/letter_avatar_proxy/v4/letter/p/f08c70/32.png) [@pluto](https://forum.hestiacp.com/u/pluto)\
**Post date:** [December 15, 2020, 7:33am UTC](https://forum.hestiacp.com/t/cron-to-purge-trash-spam-folder/2275/8 "2020-12-15T07:33:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ozgurerdogan](https://forum.hestiacp.com/letter_avatar_proxy/v4/letter/o/97f17d/32.png) [@ozgurerdogan](https://forum.hestiacp.com/u/ozgurerdogan)\
**Post date:** [December 15, 2020, 9:47am UTC](https://forum.hestiacp.com/t/cron-to-purge-trash-spam-folder/2275/9 "2020-12-15T09:47:44Z")

</div>

Thank you @pluto you gave great ideas.
