From b9f5a56ee854dc103373efc275f821d8bdb5df82 Mon Sep 17 00:00:00 2001 From: Moonsik Park Date: Wed, 5 Jun 2024 00:19:40 +0900 Subject: [PATCH] fix: recursively delete every 10000 rows to prevent running out of memory in `Request.garbage_collect` --- silk/models.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/silk/models.py b/silk/models.py index 075c5e9a..818d3eb7 100644 --- a/silk/models.py +++ b/silk/models.py @@ -166,7 +166,12 @@ def garbage_collect(cls, force=False): # Make sure we can delete everything if needed by settings if target_count <= 0: - cls.objects.all().delete() + # delete every request older than now + time_cutoff = timezone.now() + # recursively delete every 10000 rows to prevent running out of memory + while cls.objects.filter(start_time__lte=time_cutoff).count() > 0: + pks = cls.objects.filter(start_time__lte=time_cutoff)[:10000].values_list('pk', flat=True) + cls.objects.filter(pk__in=pks).delete() return try: