Delete voicemail files older than X days

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
 
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
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?
 
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.
 
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.