Delete files older than 30 days

Hello. This shell command used to work where it will delete files older than 30 days. I have recently found out that is not working.

clean_balcony_images: 'find /config/www/img/security/balcony -type f -name "*.jpg" -mtime +30 -exec rm -- "{}" \;'

There is no error in log only this shows when I run a test.

try with /homeassistant … instead of /config

PS: It was changed in last month Updates !

1 Like

ok. I have changed to this path

clean_balcony_images: 'find /homeassistant/www/img/security/balcony -type f -name "*.jpg" -mtime +30 -exec rm -- "{}" \;'

and now getting No such file or directory error.

stdout: ""
stderr: "find: /homeassistant/www/img/security/balcony: No such file or directory"
returncode: 1

hmmmm, strange, when i run the same(without rm :wink: ) ( In terminal ), i get “hits”, and can still use /config ( as it’s a symlink to /homeassistant )
However this is when im in the / of the container ( and it doesn’t matter if i have a / before homeaassistant or even (dot) /homeassistant

Try to remove /homeassistant, as it seems like the “commands” (in shell-integration) is executed within the /config (now /homeassistant)

find www/img/security/balcony -type f -name "*.jpg" -mtime +30 -exec rm -- 

above, is from within /homeassistant

I have tried with and without the /www. Not working.

  clean_balcony_images: 'find /www/img/security/balcony -type f -name "*.jpg" -mtime +30 -exec rm -- "{}" \;'

No / in-front of www

04.12.2023_13.11.45_REC

Or ./ ( read: " dot+slash " )

04.12.2023_13.21.39_REC

Still not working.

clean_balcony_images: '/homeassistant find www/img/security/balcony -type f -name "*.jpg" -mtime +30 -exec rm -- "{}" \;'

error:

stdout: ""
stderr: "/bin/sh: /homeassistant: not found"
returncode: 127

Right !.. i have no idea what you are trying to accomplice with that …
Did you read the link i posted ? , from where did you get the idea to place /homeassistant, in front of your command ?
And it was also far from what i posted pics of And “spelled out” , in commends, pic was just to “Visualize” to make sure you didn’t misunderstood . . . . . . . . . . . . . . . (dot)

Overlooked. I’ve literally copied your screenshot as if it is a shell command.
I have since tried all 3 below.

clean_balcony_images: 'find www/img/security/balcony -type f -name "*.jpg" -mtime +30 -exec rm -- "{}" \;'
clean_balcony_images: 'find /www/img/security/balcony -type f -name "*.jpg" -mtime +30 -exec rm -- "{}" \;'
clean_balcony_images: 'find ./www/img/security/balcony -type f -name "*.jpg" -mtime +30 -exec rm -- "{}" \;'

and have you checked your logfiles after each failed “attempt” ?

I get an error in log when running this. The other two. No errors

clean_balcony_images: 'find /www/img/security/balcony -type f -name "*.jpg" -mtime +30 -exec rm -- "{}" \;'

error:

error: Error running command: `find /www/img/security/balcony -type f -name "*.jpg" -mtime +30 -exec rm -- "{}" \;`, return code: 1

The link you posted says:

A 0 exit code means the commands completed successfully without error. In case a command results in a non 0 exit code or is terminated after a timeout of 60 seconds, the result is logged to Home Assistant log.

My original code and these two suggested by you of www and ./www all returned a 0. However, the files are not being deleted.

hmmm weird

It works from within the Terminal … and you are sure there is .jpg’s older than 30 days in the specified folder ?

oh wait a minute. My bad. Checking the file time stamps now. It did deleted files older than 30 days. My mind thought it should have been more. Using this works. Thank you for helping.

clean_balcony_images: 'find www/img/security/balcony -type f -name "*.jpg" -mtime +30 -exec rm -- "{}" \;'
1 Like

This should work:

shell_command:
  clean_balcony_images: find ./www/img/security/balcony/ -type f -name "*.jpg" -mtime +30 -exec rm -- "{}" \;

place it in config.yaml , then restart , then run service

PS: edited … slash after balcony/

:slight_smile: ok, good to hear, I didn’t see before i posted below, any ways works fine, but those which don’t work