Automatically delete old camera snapshots

I have several cameras using deepstack creating lots of snapshots in my www folder. What is the best way to automate deletion of images older than X days? I’ve found a few threads where someone asks and the answer is cron jobs, but no details. I’m sure someone else has already solved this problem and could provide a more detailed answer. Thanks

This is what you looking for

just create an automation…

1 Like

Thank you that did the trick. Here is my automation for anyone finding this.

- id: '7e2e07242bf24b8e9232f127cdec6646'
  alias: Delete old Snapshots
  trigger:
    platform: time
    at: "02:05:00"
  action:
    - service: delete.files_in_folder
      data:
        folder: '/config/www/backyard_object_images/'
        time: 604800
    - delay:
        seconds: 30
    - service: delete.files_in_folder
      data:
        folder: '/config/www/deck_object_images/'
        time: 604800
    - delay:
        seconds: 30
    - service: delete.files_in_folder
      data:
        folder: '/config/www/driveway_object_images/'
        time: 604800
    - delay:
        seconds: 30
    - service: delete.files_in_folder
      data:
        folder: '/config/www/front_door_object_images/'
        time: 604800
    - delay:
        seconds: 30
    - service: delete.files_in_folder
      data:
        folder: '/config/www/hot_tub_object_images/'
        time: 604800
    - delay:
        seconds: 30
    - service: delete.files_in_folder
      data:
        folder: '/config/www/sideyard_object_images/'
        time: 604800

6 Likes

Hijacking this thread because I’ve been searching for a solution for this as well.

@dusing automation didn’t work for me… so here is my solution:

add this to the config yaml: (the +3 means everything older then 3 days is deleted)

shell_command:
  removesnapshots: 'find /path/to/files/* -mtime +3 -exec rm {} \;'  

and add this to your automations yaml:

- alias: delete old images
  trigger:
    platform: time
    at: "01:00:00"
  action:
    - service: shell_command.removesnapshots
10 Likes

Thank you big time! this works great, even on Hass.io!

It is possible that if it doesn’t work it because of the ‘+’
So then you have to change:

shell_command:
  removesnapshots: 'find /path/to/files/* -mtime +3 -exec rm {} \;'  

in to:

shell_command:
  removesnapshots: 'find /path/to/files/* -mtime 3 -exec rm {} \;'  
3 Likes

stefdewilde Is this right, when I run it nothing is deleted and there is nothing in the logs.
Works! I just didn’t wait long enough.

shell_command:
  removefrontdoorsnapshots: 'find /config/www/front_door_object_images/* -mtime 7 -exec rm {} \;'
  removedrivewaysnapshots: 'find /config/www/driveway_object_images/* -mtime 7 -exec rm {} \;'
  removebackyardsnapshots: 'find /config/www/backyard_object_images/* -mtime 7 -exec rm {} \;'
  removedecksnapshots: 'find /config/www/deck_object_images/* -mtime 7 -exec rm {} \;'
  removesideyardsnapshots: 'find /config/www/sideyard_object_images/* -mtime 7 -exec rm {} \;'
  removestreetsnapshots: 'find /config/www/sideyard_object_images/* -mtime 7 -exec rm {} \;'
  removehottubsnapshots: 'find /config/www/hot_tub_object_images/* -mtime 7 -exec rm {} \;'

automation:

- id: 7e2e07242bf24b8e9232f127cdec6646
  alias: Delete old Snapshots
  trigger:
    platform: time
    at: 02:05:00
  action:
  - service: shell_command.removefrontdoorsnapshots
  - delay:
      seconds: 30
  - service: shell_command.removedrivewaysnapshots
  - delay:
      seconds: 30
  - service: shell_command.removebackyardsnapshots
  - delay:
      seconds: 30
  - service: shell_command.removedecksnapshots
  - delay:
      seconds: 30
  - service: shell_command.removesideyardsnapshots
  - delay:
      seconds: 30
  - service: shell_command.removestreetsnapshots
  - delay:
      seconds: 30
  - service: shell_command.removehottubsnapshots 
1 Like

I can’t really find an error in your setup.
Does anything happen if you run one the shell commands through ssh from your home assistant?

Sorry I needed to be patient, it just took a really really long time. All good this morning.

1 Like

don’t forget to install extension :

1 Like

This worked perfectly!!! I’ve been trying for many days! Thank you!!

hi, this works great, but does anyone know how to remove the empty picture cards from the gui after the files are deleted ?

Works perfectly for HAOS 2023.02, awesome! Thankin you!

1 Like

Did you figure this out?

-mtime +3 matches files that were modified more than 3 days ago whereas -mtime 3 matches files that were modified exactly 3 days ago.

Also you might want to use -type f to ensure you’re only working with files and -mindepth 1 so you can explicitly passing the directory instead of expanding with * which is error prone if there are no files:

shell_command:
  removesnapshots: 'find /path/to/files/  -mindepth 1 -type f -mtime +3 -exec rm {} \;'