---
title: "File"
description: "Spook adds actions that list the files in a folder Home Assistant is allowed to use, and delete them from an automation, to clean up the exports, synced backups and logs that pile up outside your media."
canonical_url: "https://spook.boo/file"
---
# File

> Spook adds actions that list the files in a folder Home Assistant is allowed to use, and delete them from an automation, to clean up the exports, synced backups and logs that pile up outside your media.

The file integration is how Home Assistant reads and writes files of its own: a notification written to a file, a sensor that reads the last line of one, the **Read file** action.

Plenty piles up in a folder outside your media. The clips a camera recorder exports, a backup folder another machine syncs into, the logs of a tool. Home Assistant cannot see what is in there, let alone clean it up.

Spook adds two actions for that: list the files in a folder, and delete them. The same as [List media and Delete media](https://spook.boo/media-source) do for your media, for the folders outside of it.

Both work in the folders Home Assistant is allowed to use, and nowhere else. Home Assistant only lets its integrations into the folders in `allowlist_external_dirs`, a list in your configuration. Out of the box, that is your media folder and the `www` folder in your configuration folder. Add your own under `homeassistant:` in your `configuration.yaml`, and restart. The folder has to be there already:

```yaml
homeassistant:
  allowlist_external_dirs:
    - /share/nvr-exports
file:
```

The `file:` line sets up the file integration, which brings these actions: they show up once it is set up. Leave it out when you set it up under **Settings** > **Devices & services** already.

## Actions

Spook adds two actions to the file integration. You find them wherever you add an action, marked with a 👻.

### List files

Lists the files in a folder, the oldest first: their full path and name, their size, and when they last changed.

With **Recursive** on, the files in the folders inside it are listed too, all the way down. With **Name pattern**, only the files whose name fits it, like `*.mp4` for the videos. Upper and lower case are the same: `*.mp4` also finds `CLIP.MP4`.

With **Older than**, only the files that last changed longer ago than that are listed. Ready to hand to [Delete files](#delete-files).

Folders, links and anything hidden (its name starts with a dot) are left out. In a synced folder, the hidden files are the sync tool's own: a cleanup should leave them be.

Listing files is for administrators only, as reading a file with the file integration of Home Assistant is.

**How to use it**

1. Open the automation or script that needs the list, and select **Add action**.
2. Search for **list files**, and select **File: List files 👻**.
3. Under **Folder**, enter the full path of the folder, like `/share/nvr-exports`.
4. Under **Response variable**, give the list a name, like `result`. The steps after this one can use it.
5. Select **Save**.

**Technical details**

| Action properties |
| --- |
| Action | File: List files 👻 |
| Action name | `file.list` |
| Action targets | No targets |
| Action response | Yes, the files in the folder |
| Spook's influence | Newly added action |
| Tools | [Try this action](https://my.home-assistant.io/redirect/developer_call_service/?service=file.list) [![Open your Home Assistant instance and show the Actions tool with a specific action selected.](https://my.home-assistant.io/badges/developer_call_service.svg)](https://my.home-assistant.io/redirect/developer_call_service/?service=file.list) |

| Action data parameters |
| --- |
| Attribute | Type | Required | Default / Example |
| `path` | string | Yes | `/share/nvr-exports` |
| `recursive` | boolean | No | `false` |
| `pattern` | string | No | `*.mp4` |
| `older_than` | string | No | `336:00:00` |

`older_than` is a duration, in hours, minutes and seconds like `336:00:00`, or in days like `days: 14` in YAML.

The response:

| **Attribute** | **Type** | **Description** |
| --- | --- | --- |
| `files` | list | The files, the oldest first. |
| `files[].path` | string | The full path of the file. The delete action takes it. |
| `files[].name` | string | The name of the file. |
| `files[].size` | integer | Its size, in bytes. |
| `files[].modified` | string | When it last changed, in ISO 8601 and UTC. |

The path is where the file really is: a folder you name through a link is listed where the link leads.

**Example actions in YAML**

Every night, delete the clips your camera recorder exported more than two weeks ago. The delete only runs when there is something to delete:

```yaml
triggers:
  - trigger: time
    at: "04:00:00"
actions:
  - action: file.list
    data:
      path: /share/nvr-exports
      recursive: true
      pattern: "*.mp4"
      older_than:
        days: 14
    response_variable: result
  - if:
      - condition: template
        value_template: "{{ result.files | count > 0 }}"
    then:
      - action: file.delete
        data:
          path: "{{ result.files | map(attribute='path') | list }}"
```

Get a notification when the backups another machine syncs in take up more than 50 GB (50 billion bytes, the way disks are sold):

```yaml
- action: file.list
  data:
    path: /share/backups
    recursive: true
  response_variable: result
- if:
    - condition: template
      value_template: >-
        {{ result.files | sum(attribute='size') > 50 * 1000 ** 3 }}
  then:
    - action: notify.persistent_notification
      data:
        title: Backups are piling up
        message: >-
          {{ result.files | count }} files, the oldest from
          {{ result.files[0].modified | as_datetime | as_local }}.
```

**Known limitations**

- **Links are left out.** A link is never deleted, so List files does not list one: nothing it lists is refused by Delete files. Whether Home Assistant may delete a file is still up to the folder it is in. A folder you name through a link is listed where the link leads.
- **Not in your configuration folder.** Even when `allowlist_external_dirs` has your configuration folder in it, or a folder above it like `/`, Spook keeps out of it: no `configuration.yaml`, no `secrets.yaml`, no database. Only a folder in there that is on the list by its own name counts, like `www`. The `.storage` folder, where Home Assistant keeps everything you set up in the UI, never does.
- **Full paths only.** A path starts with `/`, like `/share/nvr-exports`. A path without it would depend on the folder Home Assistant happens to run from.
- **For your media, use the media source.** Your media folders are allowed too, and these actions work in there. [List media and Delete media](https://spook.boo/media-source) know your media by its media source ID, the way the media browser does.
- A big folder, listed recursively, is a big response. Point it at the folder you need.
- A folder Home Assistant is not allowed to read is an error, not an empty folder: a list that misses files without a word would be worse.

### Delete files

Deletes one or more files from the folders Home Assistant is allowed to use, by their full path.

A file that is not there is skipped, not an error. Ask for the response to see which files were deleted, and which were not found.

Every path is checked before the first file goes. One path outside the allowed folders, one folder, or one link, and the action deletes nothing at all. Folders are never deleted, nor what is in them: give the files, [List files](#list-files) gives them.

When a file cannot be deleted, for example because Home Assistant is not allowed to, the action still deletes everything else. Then it fails with an error that names each file it could not delete, and why. That error is all you get back: no response, so no list of the files that did go.

Deleting files is for administrators only.

**How to use it**

1. Open the automation or script that should delete the files, and select **Add action**.
2. Search for **delete files**, and select **File: Delete files 👻**.
3. Under **File**, enter the full paths of the files to delete. **File: List files 👻** gives them.
4. Select **Save**.

**Technical details**

| Action properties |
| --- |
| Action | File: Delete files 👻 |
| Action name | `file.delete` |
| Action targets | No targets |
| Action response | Optional, the files deleted and the ones not found |
| Spook's influence | Newly added action |
| Tools | [Try this action](https://my.home-assistant.io/redirect/developer_call_service/?service=file.delete) [![Open your Home Assistant instance and show the Actions tool with a specific action selected.](https://my.home-assistant.io/badges/developer_call_service.svg)](https://my.home-assistant.io/redirect/developer_call_service/?service=file.delete) |

| Action data parameters |
| --- |
| Attribute | Type | Required | Default / Example |
| `path` | string \| list of strings | Yes | `/share/nvr-exports/front_door.mp4` |

The response:

| **Attribute** | **Type** | **Description** |
| --- | --- | --- |
| `deleted` | list of strings | The files that were there and are deleted, in the order you gave. |
| `not_found` | list of strings | The files that were not there, in the order you gave. |

**Example actions in YAML**

Delete two exported clips, and check which ones were there:

```yaml
- action: file.delete
  data:
    path:
      - /share/nvr-exports/front_door.mp4
      - /share/nvr-exports/back_door.mp4
  response_variable: result
- action: notify.persistent_notification
  data:
    message: "Deleted: {{ result.deleted | join(', ') or 'nothing' }}"
```

**Known limitations**

- **A link is never deleted.** Not the link, and not what it points at: delete the file where it really is.
- **Not in your configuration folder.** Even when `allowlist_external_dirs` has your configuration folder in it, or a folder above it like `/`, Spook keeps out of it: no `configuration.yaml`, no `secrets.yaml`, no database. Only a folder in there that is on the list by its own name counts, like `www`. The `.storage` folder, where Home Assistant keeps everything you set up in the UI, never does.
- **Full paths only.** A path starts with `/`, like `/share/nvr-exports`. A path without it would depend on the folder Home Assistant happens to run from.
- **For your media, use the media source.** Your media folders are allowed too, and these actions work in there. [List media and Delete media](https://spook.boo/media-source) know your media by its media source ID, the way the media browser does.
- **Links and `..` are followed to where they lead.** A path is checked where it really ends up, the way Home Assistant checks it. A link in an allowed folder that points out of it is refused, and so is a path that climbs out with `..`.


## Sitemap

See the full [sitemap](https://spook.boo/sitemap.md) for all pages.
