Restore unreferenced keys and mark them as deleted in the main tree