element-hq / element-hq/synapse
Deletion of url previews cache doesn't work well
- Dominant language
- Python
- Stars
- 4.6k
- Forks
- 600
- Avg merge
- 5d 22h
- Merged PRs (30d)
- 51
Description
### Description
Fresh installation of Synapse 1.139.0.
I've tested the media retention to ensure the media folder is as small as possible.
It looks like removal of url previews data from media folder is not working properly.
I tried with both automatic method (the "media_retention" option) and with manual cleanup using the /_synapse/admin/v1/media/delete api call.
Both result with a warning:
```
2025-10-03 14:42:40,950 - synapse.media.media_repository - 1545 - INFO - POST-329 - Deleting media with ID '2025-10-03_ArhIqFpBLFlGODDM'
2025-10-03 14:42:40,951 - synapse.media.media_repository - 1550 - WARNING - POST-329 - Failed to remove file: '/data/media/local_content/20/25/-10-03_ArhIqFpBLFlGODDM': [Errno 2] No such file or directory: '/data/media/local_content/20/25/-10-03_ArhIqFpBLFlGODDM'
```
```
2025-10-03 12:53:25,867 - synapse.media.media_repository - 1545 - INFO - apply_media_retention_rules-0 - Deleting media with ID '2025-10-03_VQBJSqGcSaIYVISs'
2025-10-03 12:53:25,867 - synapse.media.media_repository - 1550 - WARNING - apply_media_retention_rules-0 - Failed to remove file: '/data/media/local_content/20/25/-10-03_VQBJSqGcSaIYVISs': [Errno 2] No such file or directory: '/data/media/local_content/20/25/-10-03_VQBJSqGcSaIYVISs'
```
The file record was removed from the database, but the following files remain on disk:
* media/url_cache/2025-10-03/ArhIqFpBLFlGODDM
* media/url_cache_thumbnails/2025-10-03/ArhIqFpBLFlGODDM/480-480-image-png-scale
* media/url_cache_thumbnails/2025-10-03/ArhIqFpBLFlGODDM/32-32-image-png-crop
* media/url_cache_thumbnails/2025-10-03/ArhIqFpBLFlGODDM/240-240-image-png-scale
* media/url_cache_thumbnails/2025-10-03/ArhIqFpBLFlGODDM/96-96-image-png-crop
* media/url_cache_thumbnails/2025-10-03/ArhIqFpBLFlGODDM/550-550-image-png-scale
This is not a permission issue.
Local media (e.g. uploads) are cleaned ok.
### Steps to reproduce
Set media_retention to a short period of time, or use api /_synapse/admin/v1/media/delete to remove media older than 1 minute.
```
media_retention:
local_media_lifetime: 10m
remote_media_lifetime: 10m
```
### Homeserver
internal test server
### Synapse Version
1.139.0
### Installation Method
Other (please mention below)
### Database
sqlite
### Workers
Single process
### Platform
Debian, podman
### Configuration
_No response_
### Relevant log output
```shell
2025-10-03 14:42:40,951 - synapse.media.media_repository - 1550 - WARNING - POST-329 - Failed to remove file: '/data/media/local_content/20/25/-10-03_ArhIqFpBLFlGODDM': [Errno 2] No such file or directory: '/data/media/local_content/20/25/-10-03_ArhIqFpBLFlGODDM'
```
### Anything else that would be useful to know?
_No response_
Contributor guide
Research direction
Start in synapse.media.media_repository, following media retention and the /_synapse/admin/v1/media/delete handling for URL preview IDs. Reproduce with a short media lifetime or the delete API, then compare the paths used for the database record, url_cache, and url_cache_thumbnails. Done means the record and all listed URL-preview files are removed without the missing-file warning.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, sqlite
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100