diff --git a/src/documents/consumer.py b/src/documents/consumer.py index 31db723d9..49ce0c06a 100644 --- a/src/documents/consumer.py +++ b/src/documents/consumer.py @@ -530,24 +530,31 @@ class ConsumerPlugin( # Don't save with the lock active. Saving will cause the file # renaming logic to acquire the lock as well. # This triggers things like file renaming - document.save() + try: + document.save() + success = True + finally: + if success: + # Delete the file only if it was successfully consumed + self.log.debug(f"Deleting file {self.working_copy}") + self.input_doc.original_file.unlink() + self.working_copy.unlink() + if self.unmodified_original is not None: # pragma: no cover + self.unmodified_original.unlink() - # Delete the file only if it was successfully consumed - self.log.debug(f"Deleting file {self.working_copy}") - self.input_doc.original_file.unlink() - self.working_copy.unlink() - if self.unmodified_original is not None: # pragma: no cover - self.unmodified_original.unlink() + # https://github.com/jonaswinkler/paperless-ng/discussions/1037 + shadow_file = ( + Path(self.input_doc.original_file).parent + / f"._{Path(self.input_doc.original_file).name}" + ) - # https://github.com/jonaswinkler/paperless-ng/discussions/1037 - shadow_file = ( - Path(self.input_doc.original_file).parent - / f"._{Path(self.input_doc.original_file).name}" - ) - - if Path(shadow_file).is_file(): - self.log.debug(f"Deleting file {shadow_file}") - Path(shadow_file).unlink() + if Path(shadow_file).is_file(): + self.log.debug(f"Deleting file {shadow_file}") + Path(shadow_file).unlink() + else: + self.log.warning( + f"Document was not saved. Skipping file deletion for {self.filename}", + ) except Exception as e: self._fail(