In general, it is necessary to remove about 10,000 records from the Nodes table, only their id is not indicated explicitly, but taken from another table.
DELETE FROM Nodes WHERE (id NOT IN (SELECT DISTINCT node_id FROM Relations))
In SELECT DISTINCT id FROM @gh
, there are about 20,000 records and as a result of the deletion, the expired timeout is not performed.
Update : If you delete by package and remove DISTINCT, then another error occurs due to the lack of memory and the completion of the SQL Server process
WHILE EXISTS(SELECT * FROM Nodes WHERE id NOT IN (SELECT node_id FROM Relations)) DELETE TOP (100) Nodes WHERE id NOT IN (SELECT node_id FROM Relations)