diff --git a/src/documents/tasks.py b/src/documents/tasks.py index 2a942f4b9..055b3cc59 100644 --- a/src/documents/tasks.py +++ b/src/documents/tasks.py @@ -658,3 +658,26 @@ def build_share_bundle(bundle_id: int): temp_zip_path.unlink() except OSError: pass + + +@shared_task +def cleanup_expired_share_bundles(): + now = timezone.now() + expired_qs = ShareBundle.objects.filter( + deleted_at__isnull=True, + expiration__isnull=False, + expiration__lt=now, + ) + count = 0 + for bundle in expired_qs.iterator(): + count += 1 + try: + bundle.hard_delete() + except Exception as exc: + logger.warning( + "Failed to delete expired share bundle %s: %s", + bundle.pk, + exc, + ) + if count: + logger.info("Deleted %s expired share bundle(s)", count) diff --git a/src/paperless/settings.py b/src/paperless/settings.py index da75508d2..cfcc6692a 100644 --- a/src/paperless/settings.py +++ b/src/paperless/settings.py @@ -230,6 +230,17 @@ def _parse_beat_schedule() -> dict: "expires": 59.0 * 60.0, }, }, + { + "name": "Cleanup expired share bundles", + "env_key": "PAPERLESS_SHARE_BUNDLE_CLEANUP_CRON", + # Default daily at 02:00 + "env_default": "0 2 * * *", + "task": "documents.tasks.cleanup_expired_share_bundles", + "options": { + # 1 hour before default schedule sends again + "expires": 23.0 * 60.0 * 60.0, + }, + }, ] for task in tasks: # Either get the environment setting or use the default