Lumen - Material Pass Replace Node

I would recommend to use the Ray Tracing Quality Replace Node for this: Hardware Ray Tracing Tips and Tricks in Unreal Engine | Unreal Engine 5.0 Documentation. Keep in mind that this won’t affect screen space traces, as those trace and hit what you see on the screen.