How to create invisible object that hides objects behind it?

Is there any way to make an object invisible, but to still make it hide objects that are behind it?

It’s easier in 3D.
It’s essentially a simple shader that writes to the depth buffer but doesn’t draw anything. The graphics card will take care of not drawing objects behind it.

If it’s 2D/sprites, the shader that needs to be written will be a bit more complicated.