networkd: fix use-after-free

Elements must be removed from the hashtable before they are freed.