---
title: "Media source"
description: "Spook adds actions that list the files in a folder of your media and delete them from an automation, to clean up the camera snapshots and recordings that pile up."
canonical_url: "https://spook.boo/media-source"
---
# Media source

> Spook adds actions that list the files in a folder of your media and delete them from an automation, to clean up the camera snapshots and recordings that pile up.

The media source integration is what the media browser of Home Assistant shows. **My media** in there is the local media: the media folder of Home Assistant, and any other folder you added to it.

Automations put a lot in there. A snapshot of the front door every time somebody rings, a recording every time the camera sees movement. It only ever grows. The media browser deletes a file at a time, by hand, and an automation cannot see what is in a folder, let alone delete it.

Spook adds two actions for that: list the files in a folder, and delete them. Together, they keep a folder of snapshots to the last month, or whatever you like.

## Actions

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

Both work on My media only, by the media source ID of a file or folder, like `media-source://media_source/local/snapshots`. `local` is the media folder of Home Assistant itself. A folder you added under `media_dirs` in your configuration goes by its own name there.

### List media

Lists the media files in a folder of My media, the oldest first: their name and path, their size, when they last changed, and what kind of media they are.

The files are the ones the media browser shows: images, videos and audio. Other files, folders, and anything hidden (its name starts with a dot) are left out. With **Recursive** on, the files in the folders inside it are listed too, all the way down.

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

Leave out the folder for the top of My media. With more than one media folder, that is the top of each of them. A media folder that is not there yet has no files. A folder you name that is not there is an error.

Everybody can list media, as everybody can browse it in the media browser.

**How to use it**

1. Open the automation or script that needs the list, and select **Add action**.
2. Search for **list media**, and select **Media source: List media 👻**.
3. Under **Folder**, enter the media source ID of the folder, like `media-source://media_source/local/snapshots`.
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 | Media source: List media 👻 |
| Action name | `media_source.list_media` |
| Action targets | No targets |
| Action response | Yes, the media files in the folder |
| Spook's influence | Newly added action |
| Tools | [Try this action](https://my.home-assistant.io/redirect/developer_call_service/?service=media_source.list_media) [![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=media_source.list_media) |

| Action data parameters |
| --- |
| Attribute | Type | Required | Default / Example |
| `media_content_id` | string | No | `media-source://media_source/local/snapshots` |
| `recursive` | boolean | No | `false` |
| `older_than` | string | No | `720:00:00` |

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

The response:

| **Attribute** | **Type** | **Description** |
| --- | --- | --- |
| `files` | list | The media files, the oldest first. |
| `files[].media_content_id` | string | The media source ID of the file. The delete action takes it. |
| `files[].name` | string | The name of the file. |
| `files[].path` | string | Where it is in its media folder, like `snapshots/front_door.jpg`. |
| `files[].size` | integer | Its size, in bytes. |
| `files[].modified` | string | When it last changed, in ISO 8601 and UTC. |
| `files[].media_class` | string | What kind of media it is, as the media browser says it: `image`, `video` or `music`. |
| `files[].mime_type` | string | Its type, like `image/jpeg`. |

**Example actions in YAML**

Every night, delete the snapshots of more than a month ago. The delete only runs when there is something to delete:

```yaml
triggers:
  - trigger: time
    at: "04:00:00"
actions:
  - action: media_source.list_media
    data:
      media_content_id: media-source://media_source/local/snapshots
      recursive: true
      older_than:
        days: 30
    response_variable: result
  - if:
      - condition: template
        value_template: "{{ result.files | count > 0 }}"
    then:
      - action: media_source.delete_media
        data:
          media_content_id: >-
            {{ result.files | map(attribute='media_content_id') | list }}
```

Get a notification when the recordings take up more than 10 GB:

```yaml
- action: media_source.list_media
  data:
    media_content_id: media-source://media_source/local/recordings
    recursive: true
  response_variable: result
- if:
    - condition: template
      value_template: >-
        {{ result.files | sum(attribute='size') > 10 * 1024 ** 3 }}
  then:
    - action: notify.persistent_notification
      data:
        title: Recordings are piling up
        message: >-
          {{ result.files | count }} recordings, the oldest from
          {{ result.files[0].modified | as_datetime | as_local }}.
```

**Known limitations**

- A link to a folder is not followed into: one that points back up would never end. Name the folder it links to instead.
- A big folder, listed recursively, is a big response. Point it at the folder you need.
- A file or folder with `~` or `..` in its name is not listed. The media source of Home Assistant cannot address it, so it could not be deleted either.
- 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 media

Deletes one or more files from My media. The same as the delete in the media browser, for as many files as you like. Each goes through the media source of Home Assistant, with its own checks.

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 file is checked before the first one goes. One folder, one file of another media source like a camera, or one path that leads out of its media folder, and the action deletes nothing at all. Folders are never deleted: give the files in them, [List media](#list-media) 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.

Deleting media is for administrators only, as the delete in the media browser is.

**How to use it**

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

**Technical details**

| Action properties |
| --- |
| Action | Media source: Delete media 👻 |
| Action name | `media_source.delete_media` |
| 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=media_source.delete_media) [![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=media_source.delete_media) |

| Action data parameters |
| --- |
| Attribute | Type | Required | Default / Example |
| `media_content_id` | string \| list of strings | Yes | `media-source://media_source/local/snapshots/front_door.jpg` |

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 snapshots, and check which ones were there:

```yaml
- action: media_source.delete_media
  data:
    media_content_id:
      - media-source://media_source/local/snapshots/front_door.jpg
      - media-source://media_source/local/snapshots/back_door.jpg
  response_variable: result
- action: notify.persistent_notification
  data:
    message: "Deleted: {{ result.deleted | join(', ') or 'nothing' }}"
```


## Sitemap

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