Automation to delete files from folder older than x days

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.

7 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

sorry to bump this ancient thread…
however, it is the perfect one to ask for some help I hope

I am hoping to create that shell_command

shell_command:

  delete_snapshots: find /media/snapshots/ -mtime +90 -type f -exec rm {} \;

and automate it. Fisrts things first, so I tested this with only the -flist option, and can see the files in that share /media being found and listed alright.

However, it does not seem to make any difference how many days I add to that option, even if I do +300, it still lists all of the files (or so I think)

what’s more, if I dont use the -mtime option at all, it lists fewer files…?

Am I mis-typing there maybe?

found several suggestions here, but they all use that same option

btw, I tested this inside the add-on Terminal & SSH

edit

aargh, I think I was confused by the editors line counter… top right
if I add +600 it does in fact only show 4 files ;-).

now I only need to find a wild card on the file name

update

shell_command:

  delete_snapshots: find /media/snapshots/oprit_*.jpg -mtime +400 -type f -exec rm {} \;

thanks for the inspiration!

however:

when I use this command:

shell_command:

  delete_snapshots: >
    find /media/snapshots/oprit_*.jpg -mtime +{{states('input_number.ouder_dan')|int(600)}} -type f -exec rm {} \;

or without template and hardcoded number, and also without -type f…

wait, is it simply this: Shell Command - Home Assistant ??
Only in the /config share?
I figured running that command in the Terminal being ok would also return correct results when used as a shell_command…

Not the answer to your problem directly but have you already looked at this?

yes, thanks. I did see that. In itself it looks fine, and has quite a few options that are really helpful.

tbh, I am a bit hesitant to have file manipulation done by a CC in the first place, and especially when that has been untouched for over 2 years

I might just have to move the storage of my files to the /local folder and have core shell_command prune that .

not sure yet.

and thats what I did:

      - service: camera.snapshot
        data:
          filename: >
            /config/www/snapshots/timed/oprit_{{now().strftime('%Y_%m_%d_%H_%M')}}.jpg
        target:
          entity_id: camera.oprit

for the creation in the www folder, and allowing me to delete those like so:

shell_command:

  delete_snapshots: >
    find /config/www/snapshots/timed/ -mtime +{{states('input_number.ouder_dan')|int(600)}} -exec rm {} \;

either as a service in the automation or directly from the Dashboard

  - type: divider
  - entity: input_boolean.notify_camera_motion
  - entity: input_number.ouder_dan
  - type: button
    tap_action:
      action: call-service
      service: shell_command.delete_snapshots
    name: Delete snapshots
    icon: mdi:delete-forever

nice.

1 Like

Marius, I use a different combination. This works for me.

trim_snapshots: "find /home/homeassistant/.homeassistant/www/gallery/security_camera/ -maxdepth 1 -type f | sort -r | tail -n +6 | xargs -I {} rm -- /home/homeassistant/.homeassistant/www/gallery/security_camera/{}"
1 Like

Wow, I need to study that command….
Would you care to elaborate a bit please what it does ?

I had the -type f before, but wondered why that would be helpful in the shell_command. It can’t list anywhere? Or does it add these (file names) to the log when using that

edit

wait :wink: Let me ask ChatGPT, it should excel in that…?

Quote ChatGPT

The given Unix command is a composite command that is used to delete older files in a specific directory, keeping only the most recent five. Here’s a step-by-step breakdown of what each part does:

  1. find /home/homeassistant/.homeassistant/www/gallery/security_camera/ -maxdepth 1 -type f

    • find is a command used to search for files and directories.
    • /home/homeassistant/.homeassistant/www/gallery/security_camera/ is the directory being searched.
    • -maxdepth 1 ensures that the search is limited to the top-level directory only and does not recurse into subdirectories.
    • -type f specifies that only files (not directories) should be included in the search results.
  2. |

    • This is a pipe operator, which takes the output of the find command and passes it as input to the next command.
  3. sort -r

    • sort is a command used to sort lines of text.
    • -r option sorts the lines in reverse order (descending).
  4. |

    • Another pipe operator, passing the sorted list of files to the next command.
  5. tail -n +6

    • tail is a command that outputs the last part of files.
    • -n +6 starts output from the 6th line to the end of the input. Effectively, this skips the first five lines (the most recent five files due to reverse sorting) and lists all subsequent lines (older files).
  6. |

    • Another pipe operator, passing the remaining file list to the next command.
  7. xargs -I {} rm – /home/homeassistant/.homeassistant/www/gallery/security_camera/{}

    • xargs is a command that builds and executes command lines from standard input.
    • -I {} is an option that replaces {} with the input read from the previous command in the command line that follows.
    • rm is the command used to remove files.
    • -- is used to indicate the end of command options, so filenames starting with a dash are not treated as options.
    • /home/homeassistant/.homeassistant/www/gallery/security_camera/{} is the file path for deletion, where {} is replaced by each file path passed from the previous command.

In summary, this command chain finds all files in the specified directory, sorts them in reverse order (newest first), skips the first five files, and then deletes the remaining older files, ensuring that only the most recent five files are kept in the directory.

So at least I learnt I had it wrong thinking the -type f would mean to list all files … it is specifying only files, and not directories should be included…

ChatGPT is spot on this time. :wink:

Glad you got it sorted.