Automation to delete files from folder older than x days

So I will do it manually…not smart though…Thanks

It wouldn’t take much in an appdaemon app. I could help you create it.

1 Like

Sure :slight_smile: looking forward to…

I don’t use hass.io but use a switch configured this way:

switch:
  platform: command_line
  switches:
    clean_cctv_recordings:
      command_on: 'find /config/tensorflow/storagemotion/ -maxdepth 2 -type f -mtime +5 -exec rm {} -f \;'
      command_off: 'find /config/tensorflow/storagemotion/ -maxdepth 2 -type f -mtime +5 -exec rm {} -f \;'

It deletes all files older than 5 days.
Then all I need is to toggle the switch in an automation which runs daily. You can of course also just have a shell_command
You will also need to make sure you have whitelisted the directory:

homeassistant:
  whitelist_external_dirs:
    - /config/tensorflow/
1 Like

Thanks. I will give it a try

Yeah, I use supervised so it’s handled on my OS. Not sure if it would work in HomeAssistant hassio.

check this too: Automatically delete old camera snapshots

1 Like

Thanks. No info of working on hassio though…

I don’t know much about the differences in OSes. Mine is Home Assistant Supervised installed on Debian, so I apologize if this won’t work for you! Might work for someone else.

If your filenames contain dates like M-D-Y-H-M-S.mp4, you can use the folder sensor to trigger the automation, then run the shell_command service with something like this.

shell_command:
  delete_old_clips: 'ssh -i /config/keys/id_rsa -o StrictHostKeyChecking=no [email protected] find /usr/share/hassio/homeassistant/www/camera_motion -type f -name "{{ (as_timestamp(now()) - 864000) | timestamp_custom(''%M-%d-%y'') }}*.mp4" -delete'

Would delete files that are ten days old.

But, this might be exactly what @petro says won’t work.

I don’t believe that will work because it needs access to a ‘normal’ ssh. Worth a shot to try though.

what syntax will be for delete all files? Or files with specific extension?

that syntax deletes all files in that folder , regardless of extension
If you want to only delete some extensions, you need to add a filter to the find command, e.g. -name "*.jpg"

Maybe I wasn’t precise enough. I mean syntax which deletes all files even created a moment ago.

rm is the standard command to delete (remove) files and directories
http://manpages.ubuntu.com/manpages/trusty/man1/rm.1.html

@echojjj
Hi Jennifer,

On the same note, I understand that you are able to store the last motion-triggered clips in Home Assistant? did you pull it from Blue Iris?
Can you please share how did you achieve that?

Many thanks!! :slight_smile:

Didi

You might find a shell command will work even on HassOS. Depends on the command being used being supported in the container. You can run a console (with Portainer) and then run the command there. If it works in the container it will work in HA as well.

I have been having issues with this ever sense I switched to HASS OS from a raspbian based install.
Here is what I did to get it to work.
Add this to your configration.yaml

shell_command:
  erase_video: find /path/to/media -mtime +6 -exec rm -fr {} +

The above shell command will search in which ever folder/directory you specify for any files which have a created date more than 6 days in the past…ie, 7 days, then it will execute remove (rm) command on those files returned by the find command.

Restart HASS
create an automation with time as the trigger. I did 12:00 am.

for the action, choose call service.
The service will be shell_command.erase_video (assuming you kept it the same name).

Now every 12:00am, the automation runs and the files older than 7 days are deleted.

While HASS OS does have crontab abilities, it isn’t the easiest thing to add too…at least for me…I tried and found this a little easier to implement.

4 Likes

Does it also search in subfolders or just in the main one?

I believe it will also search in subfolders.
I took this from a Linux command that at one point had a maxdepth option appended, but it wouldn’t work with that on there.
Maybe a little tweaking is needed to define how deep in your folder structure it will go if you don’t want it to go into subfolders. It may actually delete folders that are older than the specified time period.

if you want to limit the subfolders you can add -maxdepth 1 to you command