# select\_related removed when performing a deletion

**URL:** <https://forum.djangoproject.com/t/select-related-removed-when-performing-a-deletion/40857>\
**Category:** ORM\
**Created:** [May 9, 2025, 1:10pm UTC](https://forum.djangoproject.com/t/select-related-removed-when-performing-a-deletion/40857 "2025-05-09T13:10:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaulGilmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/paulgilmartin/32/3967_2.png) [@PaulGilmartin](https://forum.djangoproject.com/u/PaulGilmartin)\
**Post date:** [May 9, 2025, 1:10pm UTC](https://forum.djangoproject.com/t/select-related-removed-when-performing-a-deletion/40857/1 "2025-05-09T13:10:06Z")

</div>

Today whilst investigating an n+1 query issue, we noticed that the following code is present in the django/db/models/query.py:

```auto
class QuerySet(AltersData):
...
    def delete(self):
        """Delete the records in the current QuerySet."""
         ...
         del_query.query.select_related = False

```

The line del\_query.query.select\_related = False appears to be removing all select\_related clauses from the queryset. This line happens _before_ we fire pre\_delete or post\_delete signals. Since these signals are applied when running queryset deletions, this means that if we have some code such as

```auto
my_query = MyModel.objects.all().select_related('my_foreign_key')
my_query.delete()

```

any signal which attempts to take advantage of the select\_related clause will now result in an n+1 query

```auto
@receiver(post_delete, sender=MyModel)
def my_signal(sender, instance, **kwargs):
    # not prefetched, so does a db look up for every instance in the my_query
    if instance.my_foreign_key:   
     ....

```

Does anyone know why we remove the select\_related clause before deletion and if there’s a way to avoid n+1 issues like this in pre\_delete/post\_delete signals?

---

<div class="post-metadata">

**Author:** ![charettes](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/charettes/32/27_2.png) [@charettes](https://forum.djangoproject.com/u/charettes)\
**Post date:** [May 9, 2025, 3:00pm UTC](https://forum.djangoproject.com/t/select-related-removed-when-performing-a-deletion/40857/2 "2025-05-09T15:00:01Z")

</div>

👋 hello there!

Looking at the blame trace for this change it seems to have been this way forever so the reasoning behind it might be lost.

I guess you could try removing it and running the test suite to determine if it breaks anything.

My hot take is that it that this line precedes the additions of deletion signals and `on_delete` as we know them today and that point the ORM was always issuing chains `DELETE` queries where `select_related` doesn’t make much sense.

If that’s the case the test suite should agree and removing it with tested support should be acceptable.

---

<div class="post-metadata">

**Author:** ![PaulGilmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/paulgilmartin/32/3967_2.png) [@PaulGilmartin](https://forum.djangoproject.com/u/PaulGilmartin)\
**Post date:** [May 12, 2025, 8:19am UTC](https://forum.djangoproject.com/t/select-related-removed-when-performing-a-deletion/40857/3 "2025-05-12T08:19:19Z")

</div>

thanks for the reply! I’ll experiment with changing it in my application and if it seems fine i’ll consider submitting a patch to django itself. Would it be appropriate to file this as a bug report?

---

<div class="post-metadata">

**Author:** ![charettes](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/charettes/32/27_2.png) [@charettes](https://forum.djangoproject.com/u/charettes)\
**Post date:** [May 12, 2025, 12:53pm UTC](https://forum.djangoproject.com/t/select-related-removed-when-performing-a-deletion/40857/4 "2025-05-12T12:53:29Z")

</div>

> [@PaulGilmartin](#):
>
> Would it be appropriate to file this as a bug report?

I think so yes, assuming [you can demonstrate the test suite passes when removed](https://docs.djangoproject.com/en/5.2/intro/contributing/#running-django-s-test-suite-for-the-first-time).

---

<div class="post-metadata">

**Author:** ![Chadys](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/chadys/32/13764_2.png) [@Chadys](https://forum.djangoproject.com/u/Chadys)\
**Post date:** [September 16, 2026, 6:23pm UTC](https://forum.djangoproject.com/t/select-related-removed-when-performing-a-deletion/40857/5 "2026-09-16T18:23:33Z")

</div>

@PaulGilmartin did you ever check this further? I have the same issue as you, I stumbled on it after using the new fetch\_mode `FETCH_RAISE` and noticing it throws in my post\_delete signal handler even though I added the appropriate select\_related. Or if anyone has an idea for a workaround?

---

<div class="post-metadata">

**Author:** ![Chadys](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/chadys/32/13764_2.png) [@Chadys](https://forum.djangoproject.com/u/Chadys)\
**Post date:** [September 17, 2026, 10:07am UTC](https://forum.djangoproject.com/t/select-related-removed-when-performing-a-deletion/40857/6 "2026-09-17T10:07:49Z")

</div>

I received some workaround ideas from the django discord:

- replace signal handler with a normal function that handles bulk deletion
- use `prefetch_related` instead, since `delete()` only clears `select_related`
- keep `select_related`, evaluate the queryset yourself and pass the list to `Collector.collect()` / `collector.delete()` so the cached related objects make it to the signal handlers

I used the `prefetch_related` solution, it’s working perfectly

---

<div class="post-metadata">

**Author:** ![jacobtylerwalls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jacobtylerwalls/32/30603_2.png) [@jacobtylerwalls](https://forum.djangoproject.com/u/jacobtylerwalls)\
**Post date:** [November 14, 2026, 1:46pm UTC](https://forum.djangoproject.com/t/select-related-removed-when-performing-a-deletion/40857/7 "2026-11-14T13:46:16Z")

</div>

@Chadys, would you be up for opening a bug report on this, and presenting the fetch modes use case? [https://code.djangoproject.com/newticket](https://code.djangoproject.com/newticket)

---

<div class="post-metadata">

**Author:** ![Chadys](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/chadys/32/13764_2.png) [@Chadys](https://forum.djangoproject.com/u/Chadys)\
**Post date:** [November 16, 2026, 7:08pm UTC](https://forum.djangoproject.com/t/select-related-removed-when-performing-a-deletion/40857/8 "2026-11-16T19:08:29Z")

</div>

Done 😉 [#37507 (select\_related removed from deletion queries) – Django](https://code.djangoproject.com/ticket/37507#ticket)
