Automate deletion of old voicemail from full Asterisk mailboxes
No matter how high the message limit, some users never delete old voicemail. That can prevent other users from forwarding messages to the full mailboxes. Here’s a quick shell script to run as a nightly cron job that deletes old messages from full or almost-full mailboxes.
#!/bin/sh
# For mailboxes with 90+ messages, delete messages older than 180 days.
set -eu
threshold=90
days=180
vmroot=/var/spool/asterisk/voicemail/default
find "$vmroot"/*/Old -maxdepth 1 -type d \
-exec sh -c "echo -n {} ' ' ; find {} -type f -name 'msg*wav' | wc -l" \; \
| awk "{ if (\$NF > $threshold && NF == 2) printf \"%s \", \$1 }" \
| xargs -I% sh -c "find % -type f -name 'msg*' -mtime +$days \
-exec rm {} \;"
This purges messages older than 180 days from mailboxes with 90+ messages.
We can also adapt these bits for ad-hoc use, like:
$ find /var/spool/asterisk/voicemail/default/*/Old -maxdepth 1 -type d \
-exec sh -c "echo -n {} ' ' ; find {} -type f -name 'msg*wav' | wc -l" \; \
| awk '{ if ($NF > 90) print }'
/var/spool/asterisk/voicemail/default/100/Old 100
/var/spool/asterisk/voicemail/default/123/Old 100
/var/spool/asterisk/voicemail/default/341/Old 97
/var/spool/asterisk/voicemail/default/471/Old 100
/var/spool/asterisk/voicemail/default/561/Old 100
#asterisk #shell