There are many reasons you might want or need to change your rear derailleur. First of all, it might have snapped in a crash. Alternatively, it might simply have worn out from hundreds of gear changes.
Derailleur pivots will wear over time, causing sluggish shifting as the guide jockey struggles to respond to changes in cable tension. Take hold of the bottom of the derailleur cage and rock it sideways, towards and away from the back wheel. A bit of flex is fine, but if you can feel the cage knocking, replace the derailleur. This task is easiest when you have as much chain slack to play with as possible. That’s why you should change into the smallest sprocket at the back, while at the same time dropping the chain off the smallest sprocket at the front and into the gap between the chainset and the frame. Clip the end-cap off the cable.
“A bit of flex is fine, but if you can feel the cage knocking, replace the derailleur”
Step 1: Undo the cable clamp bolt and unthread the cable from the barrel-adjuster. If you reuse the same cable, there is no need to unthread it further, although you may find that a new derailleur will have a different configuration and need a longer cable. It is best to split the derailleur to remove the chain from it, as splitting
the chain takes longer and weakens the split link. First, undo the guide jockey bolt slightly.
Step 2: Remove the tension jockey bolt, which is the one in the middle of the lower of the two jockey wheels.
Step 3: Slide the tension jockey wheel forward. It has a washer on each side. Don’t lose these; they tend to drop out as you pull the jockey wheel out.
Step 4: Rotate the back of the cage slightly so that you can slide the chain out of the cage without splitting the chain. Undo the fixing bolt that attached the derailleur to the frame. Discard the old derailleur.
Step 5: Remove the tension jockey wheel on the new rear derailleur and loosen the guide jockey bolt. Hold the derailleur upside down, as shown. Fit the derailleur to the lower loop of chain, laying it over the guide jockey then trapping it in place with the tension jockey. Make sure it passes inside both tabs (A), so that the chain runs between the tab and the jockey wheel. Refit the tension jockey bolt. Tighten both jockey wheel bolts firmly.
Step 6: Turn the derailleur clockwise and pull it backwards, so the fitting bolt is aligned with the frame-hanger. Bolt the derailleur to the frame, ensuring the bolt does not cross-thread. It’s important that the small B-tab behind the derailleur sits behind the matching tab on the lower part of the frame-hanger, stopping the derailleur from swinging too far forward.
Source : BIKE MAINTENANCE TIPS, TRICKS & TECHNIQUES
See also bike maintenance tips, tricks and techniques “Rear derailleur varieties”