SELECT ReplicaID FROM CacheReplicas WHERE Links = 0 AND ReplicaID NOT
IN ( SELECT DISTINCT( ReplicaID ) FROM StageRequests )
followed by
DELETE FROM CacheReplicas WHERE ReplicaID IN (%s) AND Links= 0;	
will cause a race condition under certain conditions when old tasks are
set failed.