Delete voicemail files older than X days

Status
Not open for further replies.

simcard

Member
Jan 22, 2017
49
4
8
If you have a reasonably active FusionPBX (or vanilla Freeswitch) installation and you allow users to keep voicemail on the server, you're probably seeing your voicemail storage grow and available disk space dwindle. I thought this would be handy to share.

Our ultimate goal is to move voicemail storage into the cloud, but this little snippet is helping us bridge the gap until we get there.

Most, if not all telco's I've seen have an expiry on their voicemails (i.e. the user can't just leave them there for an eternity), somewhere around 30-60 days. I came across the following which finds voicemail files that are older than a certain date with the option of deleting them.

Source: http://blog.ones-app.com/how-to-recursively-find-and-delete-files-from-server-after-certain-time/

(I've updated the voicemail directory based on our freeswitch/fpbx installs, best to check where yours are located).

If you want to simply list the voicemail files that are 30 days or older, use:
Code:
find /var/lib/freeswitch/storage/voicemail -type f -name "msg_*" -mtime +30 -exec ls -lhtr {} \;

If you want to delete the voicemail files that are 30 days or older, use:
(THIS WILL DELTE FILES - BE CAREFUL)
Code:
find /var/lib/freeswitch/storage/voicemail -type f -name "msg_*" -mtime +30 -exec rm {} \;

If you want to change the number of days, simply change '-mtime +30' to for example' -mtime +60' for 60 days

Simply add this as a cron and run however often you need. We run our once a day early in the morning so as not to disrupt customers.

Code:
crontab -e

add:
0 0 * * * find /var/lib/freeswitch/storage/voicemail -type f -name "msg_*" -mtime +30 -exec rm {} \;

Will delete all voicemails older than 30-days, every 24 hours at midnight (server time).

cheers,

A
 

simcard

Member
Jan 22, 2017
49
4
8
So what happens to the orphaned DB entries?

Good pick up

I've rewritten the script in PHP to list the messages from the DB which have a "created_epoch" older than 'x' days, which then deletes those from both the filesystem and DB. Still in testing at the moment and will release when we're happy with it.

However, your reply raises more questions for those in Oz with the meta data laws. From memory we're not required to keep the actual voicemail recording itself, just the details around when it was stored and any retrievals etc (i.e. the 'communication', but not the 'content'). Will need to revisit the specifics on meta data and voicemail to be 100%.

If I am correct re: meta data; changing the "message_status" for each voicemail in the 'v_voicemail_messages' table to something like 'deleted', rather than deleting the entry, will allow us to keep a record of the message details while not showing up in the Voicemail section or retrieval via handset in FusionPBX (looking through the code it only looks for voicemails with a "message_status" of null, ' ' or 'saved').

We'd then be able to delete the voicemail recording file to achieve our desired outcome of minimizing disk space used by voicemails. The DB entry would still technically be orphaned, however shouldn't be referenced (at least in v4.2)

Thoughts?


Could well worth be turning this into an App (i.e. recording/voicemail etc retention/cleanup) at some point, once I've got my head around it a little more.
 

EasyBB

Active Member
Oct 23, 2016
240
33
28
Australia
I've rewritten the script in PHP to list the messages from the DB which have a "created_epoch" older than 'x' days, which then deletes those from both the filesystem and DB. Still in testing at the moment and will release when we're happy with it.
Really appreciate your effort to add useful functionality. There is already a delete voicemail function in one of the php files as part of FusionPBX, so all you need is to pass the message id to that function?
 

simcard

Member
Jan 22, 2017
49
4
8
Really appreciate your effort to add useful functionality. There is already a delete voicemail function in one of the php files as part of FusionPBX, so all you need is to pass the message id to that function?

I did notice those vm functions while trawling through the code, however went without it in the interest of time.

Are there any docs or info anywhere that would help us get started with semi-integrating and using those functions? We've taken a look at Luis' blog post about the 'app anatomy', which is helpful however I feel this feature alone is not 'app worthy'.

We're pretty handy with PHP, but we're obviously less familiar with how FPBX is written, so any basic level of info would be ideal - we'd love to give back to the community as this software has helped us significantly.
 

benofishal

New Member
Sep 27, 2018
2
0
1
40
Hi did you ever get this feature into FusionPBX? I would be interested in auto purging voicemail files/database entries after x no of days but not sure how to go about it. Any guidance would be greatly appreciated.
 

zenvoip

Member
Jul 19, 2018
36
2
8
58
I want to delete the voicemail files that are 30 days or older, but I am not able to figure this out, I am not linux guy I am learning..
my storage is 70%.. Thanks in advance..
 

Kenny Riley

Active Member
Nov 1, 2017
243
39
28
36
I want to delete the voicemail files that are 30 days or older, but I am not able to figure this out, I am not linux guy I am learning..
my storage is 70%.. Thanks in advance..

There is a maintenance script included with FusionPBX that will do this. By default it is set to clean up voicemails older than 90 days but you can set it for 30 if you'd like.

Code:
sudo nano /etc/cron.daily/fusionpbx-maintenance.sh

Find this:
Code:
#delete voicemail older than 90 days
if [ .$switch_package = .true ]; then
        echo ".";
        #find /var/lib/freeswitch/storage/voicemail/default/*  -name 'msg_*.wav' -mtime +90 -exec rm {} \;
        #find /var/lib/freeswitch/storage/voicemail/default/*  -name 'msg_*.mp3' -mtime +90 -exec rm {} \;
else
        echo ".";
        #find /usr/local/freeswitch/storage/voicemail/*  -name 'msg_*.wav' -mtime +90 -exec rm {} \;
        #find /usr/local/freeswitch/storage/voicemail/*  -name 'msg_*.mp3' -mtime +90 -exec rm {} \;
fi

Uncomment out the lines to perform these actions and change the entries of 90 to 30:

Code:
#delete voicemail older than 30 days
if [ .$switch_package = .true ]; then
        echo ".";
        find /var/lib/freeswitch/storage/voicemail/default/*  -name 'msg_*.wav' -mtime +30 -exec rm {} \;
        find /var/lib/freeswitch/storage/voicemail/default/*  -name 'msg_*.mp3' -mtime +30 -exec rm {} \;
else
        echo ".";
        find /usr/local/freeswitch/storage/voicemail/*  -name 'msg_*.wav' -mtime +30 -exec rm {} \;
        find /usr/local/freeswitch/storage/voicemail/*  -name 'msg_*.mp3' -mtime +30 -exec rm {} \;
fi

Then you will need to add the maintenance script to run daily using a crontab. This will make the script run nightly at 9PM.

Code:
sudo crontab -e
* 21 * * * bash /etc/cron.daily/fusionpbx-maintenance.sh

To force the script to run manually just browse to the directory of where the maintenance script resides and execute it..

Code:
cd /etc/cron.daily
./fusionpbx-maintenance.sh
 
Last edited:

zenvoip

Member
Jul 19, 2018
36
2
8
58
So is there a easy way to store VM off the phone sever? I saw few threads on it ?But did not see any then concrete on how to it.
 

ad5ou

Active Member
Jun 12, 2018
884
195
43
Easy depends on your skill set. There is no single solution hence the multiple threads showing how others have done it.

The problem is, if you are attempting to save recordings in real time to any type of remote server it will be slower than saving to the local file system and if the remote file system goes away for some reason Freeswitch will most likely crash.

The only reliable way to store recordings remotely is to save locally first then use some method to move the files and update database with new location after the initial file is saved.
 
Status
Not open for further replies.