Delete files and folders older than 30 days

Can anyone tell me if it is possible to delete files and folders they was older
than 30 days in the /share folder?

My IPCam store the video files in the /share order and it should be automatically deleted files and folders older than 30 days.

BR Tom

I would take a hunch that you could get a list of files using something like

Personally, because my HA is running on a virtual machine, and all the files I care about are actually on the (windows) host, I’d use something like a batch file to generate the output I need, and pipe it to a text file, then read that into HA to process. I’ve done this before for other use cases.

On Linux for files:

find <path> -type f -mtime +30 -exec /bin/rm -f {} \;

Define it as a shell command that you can invoke from an automation.

Can you explain this please? I‘m a HA Rookie :wink:

My sytem run on a PI 4 on a SSD.

If your HA configuration is sliced you most probably have a shell_commands.yaml where then you can add a new line (e.g. for removing old files from under /home/pi/logs) :

remove_old_files: 'find /home/pi/logs -type f -mtime +30 -exec /bin/rm -f {} \;'

then in the automations directory create a new automation remove_old_files.yaml:

alias: "Remove old files"
initial_state: "on"
id: remove_old_files
trigger:
  platform: time
  at: "22:00:00"
action:
  - service: shell_command.remove_old_files

then restart HA and at 22:00 the automation will trigger the removal of old files.
The find comamnd above works the following way: it takes all files recursively below the configured directory (e.g. /home/pi/logs), will apply the filter -mtime +30 which selects only files whose modification time is older than 30 days and will execute the command provided by -exec, in the above case removal of those files. Without the -exec part it will simply list them.
For more information on find you may use man find in a terminal on your Pi.

Thank you for your help.

I will try it in the next days.

Is there a way to check the folder size, and to delete them in blocks based on that?

For example, at 22:00:00 each night it will check to see if the folder has exceeded Xgb, and then to delete 1 day’s worth of data, and then continue looping around until the folder size get’s back to Xgb, so that rather than having 30 day’s worth of data you have Xgb (plus whatever was recorded before 22:00:00)?

I’m not sure that this can be done, but it might be useful to some people wanting to replicate the “keep until space is needed” function that traditional NVR have.

take a look at this. this might do what you want:

look at the delete.files_in_folder with the time attribute to specify the age

1 Like

I think that this will likely do exactly what I’m looking for

  • size Indicates maximum folder size to be kept in Megabytes (value 0 disables this functionality). After checking last modification time additional check is made towards folder size. If it exceeds the specified value, then files are deleted even if specified time has not been exceeded. Files are deleted starting from the oldest file until the condition is met. It is an optional attribute.

I’ve got this up and running, a must for anyone who wants to use HA an NVR.

Up vote for getting this included in the vanilla build.