I got that working, see http://t3test.xetpoint.com/media/development/delete_mm_relations4.txt - seaching possible lost relations need a loop, but now SELECT queries from the main table have been done only from such mm table records, which has uid_local in the mm table.