I have a table with a lot of history entries that contain customer IDs.
There is a separate customer table. Occasionally some of the customer entries are removed.
DELETE h.* FROM history h LEFT JOIN customer c ON h.customer_id = c.id WHERE c.id IS NULL
I'm typing this from the top of my head, but you get the idea hopefully.
Delete syntax documentation