Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] document_renamer does not update media filename metadata
### Description

When running `document_renamer` to apply a change to `PAPERLESS_FILENAME_FORMAT`, the tool runs successfully and files are moved as expected. However, in the Web UI viewing the `Metadata` tab shows a `Media filename` of the old path of the document, and not the new one as expected.

### Steps to reproduce

1. Have a document imported under one `PAPERLESS_FILENAME_FORMAT`
2. Change `PAPERLESS_FILENAME_FORMAT` to something else & restart container
3. Run `document_renamer`, wait for success
4. Visually verify that the file has been correctly renamed
5. In the Web UI, go to the `Metadata` tab for the document, see that the `Media filename` entry still reflects the previous filename

### Webserver logs

```bash
Nothing interesting, but included:

[2024-02-21 01:05:00,189] [INFO] [paperless.classifier] No updates since last training

[2024-02-21 01:05:00,220] [DEBUG] [paperless.tasks] Training data unchanged.

[2024-02-21 02:05:00,273] [DEBUG] [paperless.classifier] Gathering data from database...

[2024-02-21 02:05:00,292] [INFO] [paperless.classifier] No updates since last training

[2024-02-21 02:05:00,323] [DEBUG] [paperless.tasks] Training data unchanged.

[2024-02-21 03:05:00,186] [DEBUG] [paperless.classifier] Gathering data from database...

[2024-02-21 03:05:00,206] [INFO] [paperless.classifier] No updates since last training

[2024-02-21 03:05:00,238] [DEBUG] [paperless.tasks] Training data unchanged.

[2024-02-21 04:05:00,172] [DEBUG] [paperless.classifier] Gathering data from database...

[2024-02-21 04:05:00,192] [INFO] [paperless.classifier] No updates since last training

[2024-02-21 04:05:00,223] [DEBUG] [paperless.tasks] Training data unchanged.

[2024-02-21 05:05:00,219] [DEBUG] [paperless.classifier] Gathering data from database...

[2024-02-21 05:05:00,238] [INFO] [paperless.classifier] No updates since last training

[2024-02-21 05:05:00,270] [DEBUG] [paperless.tasks] Training data unchanged.

[2024-02-21 06:05:00,166] [DEBUG] [paperless.classifier] Gathering data from database...

[2024-02-21 06:05:00,186] [INFO] [paperless.classifier] No updates since last training

[2024-02-21 06:05:00,217] [DEBUG] [paperless.tasks] Training data unchanged.

[2024-02-21 07:05:00,171] [DEBUG] [paperless.classifier] Gathering data from database...

[2024-02-21 07:05:00,190] [INFO] [paperless.classifier] No updates since last training

[2024-02-21 07:05:00,225] [DEBUG] [paperless.tasks] Training data unchanged.

[2024-02-21 07:51:45,222] [DEBUG] [paperless.management.consumer] Consumer exiting.

[2024-02-21 07:51:55,054] [INFO] [paperless.management.consumer] Polling directory for changes: /usr/src/paperless/consume

[2024-02-21 08:05:00,153] [DEBUG] [paperless.classifier] Gathering data from database...

[2024-02-21 08:05:00,172] [DEBUG] [paperless.classifier] 4 documents, 1 tag(s), 2 correspondent(s), 0 document type(s). 0 storage path(es)

[2024-02-21 08:05:00,172] [DEBUG] [paperless.classifier] Vectorizing data...

[2024-02-21 08:05:00,808] [DEBUG] [paperless.classifier] Training tags classifier...

[2024-02-21 08:05:01,445] [DEBUG] [paperless.classifier] Training correspondent classifier...

[2024-02-21 08:05:02,080] [DEBUG] [paperless.classifier] There are no document types. Not training document type classifier.

[2024-02-21 08:05:02,080] [DEBUG] [paperless.classifier] There are no storage paths. Not training storage path classifier.

[2024-02-21 08:05:02,131] [INFO] [paperless.tasks] Saving updated classifier model to /usr/src/paperless/data/classification_model.pickle...

[2024-02-21 08:11:15,263] [DEBUG] [paperless.management.consumer] Consumer exiting.

[2024-02-21 08:11:24,322] [INFO] [paperless.management.consumer] Polling directory for changes: /usr/src/paperless/consume
```


### Browser logs

_No response_

### Paperless-ngx version

2.5.3

### Host OS

Docker Official Image, Kubernetes, x86_64

### Installation method

Docker - official image

### Browser

Chrome

### Configuration changes

_No response_

### Other

_No response_

### Please confirm the following

- [X] I believe this issue is a bug that affects all users of Paperless-ngx, not something specific to my installation.
- [X] I have already searched for relevant existing issues and discussions before opening this report.
- [X] I have updated the title field above with a concise description.


Discussion:
I assume youre just seeing a cached version. Can you try disabling or clearing browser cache and / or restarting the container?
Good call. Refreshing the page with the cache disabled in the developer tab yielded a fresh (correct) value.

Thanks!

Resolving patch:
diff --git a/src/documents/caching.py b/src/documents/caching.py
--- a/src/documents/caching.py
+++ b/src/documents/caching.py
@@ -189,6 +189,11 @@ def refresh_metadata_cache(
     cache.touch(doc_key, timeout)
 
 
+def clear_metadata_cache(document_id: int) -> None:
+    doc_key = get_metadata_cache_key(document_id)
+    cache.delete(doc_key)
+
+
 def get_thumbnail_modified_key(document_id: int) -> str:
     """
     Builds the key to store a thumbnail's timestamp
diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py
--- a/src/documents/signals/handlers.py
+++ b/src/documents/signals/handlers.py
@@ -22,6 +22,7 @@
 from filelock import FileLock
 
 from documents import matching
+from documents.caching import clear_metadata_cache
 from documents.classifier import DocumentClassifier
 from documents.consumer import parse_doc_title_w_placeholders
 from documents.file_handling import create_source_path_directory
@@ -434,7 +435,9 @@ def update_filename_and_move_files(sender, instance: Document, **kwargs):
             Document.objects.filter(pk=instance.pk).update(
                 filename=instance.filename,
                 archive_filename=instance.archive_filename,
+                modified=timezone.now(),
             )
+            clear_metadata_cache(instance.pk)
 
         except (OSError, DatabaseError, CannotMoveFilesException) as e:
             logger.warning(f"Exception during file handling: {e}")


Test patch: