Fix delete_all when chained with joins.

Backported fix from master for issues #7287, #5202 and #919. Would it be possible to include this in 3-2-stable?