How to delete all records which are not referenced from other tables
I can think of no better way than doing:
DELETE FROM referenced_table AS r
WHERE NOT EXISTS (SELECT 1 FROM referencing_table_1
                  WHERE ref_id = r.id)
  AND NOT EXISTS (SELECT 1 FROM referencing_table_2
                  WHERE ref_id = r.id)
  AND ...;

This is a very unusual requirement.
If you need to do that often, you could even consider denormalizing your database:

add a column reference_count to the referenced table

define triggers that update reference_count whenever rows in the referencing tables are added, deleted or modified


Then you can simply delete the rows with reference_count = 0. Processing will be fast (if all your foreign keys are indexed!!), but you buy that at the price of slowing down all data modifications on all referencing tables.