Do not touch nodes that do not exist

Fixes #18289.

(cherry picked from commit f42aef37fd852e0ec3eb773b62f6b1fc527387dd)