Backpropagation backpropagates from the variable you call backprop from to leaf variables.
Leaf variables are input nodes to the graph.  Intermediate variables keep graph history.
When you call detach you are kind of breaking the graph and creating a new fresh leaf variable so that memory is reused. Else, it stills carry old graph I would say.
And it’s not x=detach(x) but x=x.detach()  Detach is a tensor method. I dunno if it exists are a standalone function in torch