What you need is Spanning tree protocol, turn on this function in your switch.
It's a loop-free topology for any bridged Ethernet local area network.
This protocol will disables links that are not part of the spanning tree, leaving a single active path between any two network nodes.
We often use RSTP(802.1w) instead of STP(IEEE 802.1D), for the previous provides faster spanning tree convergence after a topology change.
If you use VLAN in your environment, you may choose MSTP(Multiple Spanning Tree Protocol).