Skip to content

Commit

Permalink
fix: recursively delete every 10000 rows to prevent running out of me…
Browse files Browse the repository at this point in the history
…mory in `Request.garbage_collect`
  • Loading branch information
moonsikpark committed Jun 4, 2024
1 parent 1cb4623 commit b9f5a56
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion silk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit b9f5a56

Please sign in to comment.