Delete files service

delete: in configuration.yaml initializes the integration, similar to other integrations, where data has to be in configuration.yaml for them to work. Only services delete.file_in_folder and delete.file are part of the integration, nothing else. Services would be visible after the integration is started (after adding folder in custom_components and data to configuration.yaml and restart is done to cache/register python files within HA)

1 Like

Thank you both @DivanX10 and @chomu , works flawlessly, does its job, simple, plain, beatufil.

I love this community.

Hi Jodur
Ive just come across your integration and Im using it to create a MP4 from jpegs this works great, then im trying to movethe jpgs to another folder but I cant get it to work
any ideas

alias: move jpegs
sequence:
  - service: imagedirectory.move_files
    data:
      sourcepath: /media/front/photo
      exclude: front_latest.jpg
      destinationpath: /media/front
mode: single

Works

alias: jpeg to Mp4 media exclude latest
sequence:
  - service: imagedirectory.create_gif_mp4
    data:
      sourcepath: /media/front/photo
      exclude: front_latest.jpg
      destinationpath: /media
      format: mp4
mode: single

@gazzaman i had a look in the code and found a typo-bug in the endtime default parameter. This is fixed in a new release, so if you update to the latest version, this should work now!

1 Like

Thanks all working now

   Hello, i have searched for such service and i am glad you have created one. What i don't get it to do is to delete files from outside config folder. Is it possible?

   Thank you for your work

You can create a service using “Shell Command”. All you need is to add the following to your configuration.yaml file:

shell_command:
   delete_file: rm /config/www/camera/snapshots/image.jpg

Restart HA and you will have the following service: shell_command.delete_file

You can create as many services as you want.

shell_command:
   delete_snap1: rm /config/www/camera/snapshots/image.jpg
   delete_snap2: rm /config/www/camera/snapshots/image2.jpg
   rename_snap: mv /config/www/camera/snapshots/image.jpg /config/www/camera/snapshots/renamed.jpg 

And so on…

5 Likes

@fberbert 's solution is the most straightforward. I just added the following to my configuration.yaml to give me a service I can call to delete everything more than 48 hours old in my /config/backups/ directory:

shell_command:
  purge_backups_older_than_two_days: find backups/ -mtime +2 -delete -print

I have an automation that triggers once per hour to create a new backup and execute the above purge:

alias: Hourly Backups
description: ""
trigger:
  - platform: time_pattern
    minutes: "0"
condition: []
action:
  - service: backup.create
    data: {}
  - service: shell_command.purge_backups_older_than_two_days
    data: {}
mode: single

I just set this up so we’ll see how it goes but I think this should do the trick nicely without having to add any other HACS/add-ons for it. Thanks @fberbert !

EDIT: corrected a problem with my shell command above

EDIT2: corrected another problem with my shell command above. It actually executes from within the config directory.

What’s happened to the remote file delete option. I only see the delete file and delete files in folder option under services?
I need to create a task to delete old file from my NAS.
Thanks

I’m trying to use this custom service to delete files from backup folder, but it gives me an error below:

Error while executing automation automation.daily_backup_housekeeping_1_week: Mandatory key “folder” is missing. @ data[‘folder’]

My config:

service: delete.files_in_folder
data:
  folder: /backup/
  time: 604800
  only_extensions:
    - .tar

Can anyone help to advise?

hello I can ask you for help, I should do a command like what you did but that deletes a folder inside the directory media / frigate thanks

This worked for me except I missed step 6 at first.

@chomu great integration, if you could fix the reason why you need to do this additional step of adding to the config it would be absolutely awesome.

Needs a WTH to get it added to core. @frenck - how about it?

Have you found a solution? I get the same error: Fehler beim Aufrufen des Diensts delete.files_in_folder. Mandatory key “folder” is missing. @ data[‘folder’]. Got None

service: delete.files_in_folder
data:
folder: ‘config/downloads/’
time: 604800
scan_subfolders: true
only_extensions: ‘.mp4’

Mandatory key "folder" is missing. @ data['folder']. Got None

Getting same error as the last couple of people. Was a fix ever found for this?

Disregard, I was able to figure out that I had to edit in YAML instead of the visual editor for my automation. Hope that will help someone else who runs across this thread.

Thanks. Exactly what I have been loking for after I couldnt manage to do shell commands

Hello,

I was just wondering if this integration can delete files in folders with wildcards. Since 2024.02 version when the database purges it creates a home-assistant_v2.db.corrupt.xxxxxxx (where xxxxxxx is the timestamp).

I would like to use this integration to delete these files but trying as below it didn’t work for me:

service: delete.files_in_folder
data:
  folder: '/config/'
  time: 0
  only_extensions:
    - '.fault'
    - '.1'
    - '.db.corrupt*'

Am i doing something wrong or the windcard is not supported by the integration?

Thanks

Hi,
Hoping someone might be able to help. I’ve added the add-on, however can’t work out which file I need to place my call service into.

service: delete.files_in_folder
data:
folder: ‘/config/www/doorbell/’
time: 36000

I tried adding it to config.yaml and also automations.yaml but no joy.

not supported

any idea why this is not working for me? i only want to delete JPG files in this media folder
image

alias: auto delete all JPG files
description: ""
trigger:
  - platform: time
    at: "00:00:00"
condition: []
action:
  - service: delete.files_in_folder
    data:
      folder: '/config/www/media'
      only_extensions:
        - '.jpg'
mode: single

deleting 1 JPG file works as seen here

alias: auto delete a JPG files
description: ""
trigger:
  - platform: time
    at: "00:00:00"
condition: []
action:
  - service: delete.file
    data:
      file: /config/www/media/alley.jpg
mode: single