FreePBX Automatically Delete Recorded Calls

If you are recording calls on FreePBX you will need to delete the recording files at some point, else your hard drive will fill up. Recordings for FreePBX are started in /var/spool/asterisk/monitor. You can use a program like WinSCP to browse to the files and delete the old files or you can setup a script to do this for you. Below is a simple script that will search out old files and delete them. It will then search and delete any old directory in the folder to keep your system directory from having folders for years/months/days that you do not have recordings for.

First you will need to create a create a bash file.

Type:
nano -w /usr/local/sbin/clear-recordings.sh

Next Past in code below:

_______________________________________________________________________
#!/bin/bash

# Author: Patrick Collins
# Date: 2022/Nov/07

#Find old recordings and delete them
find /var/spool/asterisk/monitor/ -type f -mtime +365 -exec rm -f {} \;

#Delete old empty directory
find /var/spool/asterisk/monitor/ -type d -empty -delete
_________________________________________________________________________

Save and exit!

Now That we have the script we will need to make it executable by typing: 
chmod +x /usr/local/sbin/clear-recordings.sh

Test your script by typing:
runuser -l asterisk -c '/usr/local/sbin/clear-recordings.sh'

Next you will need to run the script on a schedule. 
To do that you will need to add the script to Crontab using the following commands. 
nano -w /etc/crontab

Add the following line to the bottom of the file. 
0 0 1 * * asterisk /usr/local/sbin/clear-recordings.sh

Save and exit!

  


Leave a Reply

Your email address will not be published. Required fields are marked *