How to clean up old recorded calls?

I’m using the below code to record calls:

exten => _901.,1,Set(DIRNAME=${CALLERID(num)}/${STRFTIME(${EPOCH},,%Y%m/%d)})
exten => _901.,2,Set(FILENAME=${DIRNAME}/${STRFTIME(${EPOCH},,%H%M%S)}-${EXTEN:2})
exten => _901.,3,System(/bin/mkdir -p /var/spool/asterisk/monitor/${DIRNAME})
exten => _901.,4,MixMonitor(${FILENAME}.gsm,b)
exten => _901.,5,Dial(SIP/${EXTEN:2},20)

and it works fine. I’m looking for a way such as a bash or perl script that I can use to delete the recordings once they reach a certain age. I’ve seen scripts to do this for single directories, but the above code creates a somewhat elaborate directory structure in which the calls are stored. I found this code here Any thoughts are welcome.

In the past I’ve used the find command piped to xargs to accomplish what you want. Schedule it in cron to run daily.

You can use the linux “at” command to schedule an action in the future

exten => s,n,System(echo rm //|at now + 14 days)

This is not a tested line but I have used this kind of lines it in the same way so I assume it will work. If you add this line the soundfile will be deleted over 2 weeks. If it doesn’t work try it without the echo at the beginning.

Do “man at” on the linux prompt for more info. I hope this is helpful

That works - thanks! I was familiar with the ‘at’ command but did not think about using it to schedule removal of the file right from extensions.conf - clever!

Glad to hear that it works :wink:

I have no idea if there are any limitations in the number of “at” action that can be scheduled. If this is an important feature my advice is to test this. It will work with 100 and probably 1000 scheduled action but I have no idea if it will work whith 1000 calls a day with a scheduled removal of 30 days (so 30.000 at actions will be scheduled) .

If there are limitations you can schedule one “rm” per day for the directory where the recordings of a specific day are stored. It just need some extra lines of coding.

With the asterisk “stat” command you can check if a directory is already there so the first call of a day can trigger the creation of the dir for the recordings of that day and can schedule the removing of that directory after the period of your choice. This way you avoid a to large number of scheduled “at” actions.

enter “core show function STAT” on the cli for info about the stat command

exten => s,n,Set(DIR_EXISTS=${STAT(d,/var/spool/asterisk/monitor/30-09-2008)}) ; example that checks if there is a 30th september dir

Try something like this from cron. Run the find command 1st to make sure it is what you want before you pipe it to xargs to do the rm.