Theory:

Extracting Metals towards the Top of the activity Series
Metals at the top of the reactivity scale are extremely reactive. By heating with carbon, they cannot be extracted from their compounds. For example, carbon cannot convert oxides of sodium, magnesium, calcium, aluminium, etc. to their respective metals. This is due to the fact that these metals have a higher affinity for oxygen than carbon. Electrolytic reduction is used to obtain these metals. For example, sodium, magnesium, and calcium are obtained via electrolysis of their molten chlorides. Metals are deposited at the cathode (the negatively charged electrode), while chlorine is released at the anode (the positively charged electrode). The reactions are as follows:
 
At cathode: Na++eNa
 
At anode: 2ClCl2g+2e
 
Similarly, electrolytic reduction of aluminium oxide yields aluminium. The metals produced by the above-mentioned reduction methods are not very pure. They contain impurities that must be eliminated before pure metals can be obtained. Electrolytic refining is the most common method for purifying impure metals.
Electrolytic refining
Copper, zinc, tin, nickel, silver, gold and other metals are purified electrolytically. The anode is constructed of impure metal, whereas the cathode is made of a narrow strip of pure metal. An electrolyte is a solution of metal salt. The pure metal from the anode dissolves into the electrolyte as the current is passed through it. On the cathode, an equivalent amount of pure metal from the electrolyte is deposited. The soluble impurities dissolve in the solution whereas the insoluble impurities gets collected at the bottom of the anode which is called anode mud.
 
1.png
Electrolytic refining of metals
Reference:
https://etn-socrates.eu/wp-content/uploads/2018/08/Cu-electrorefining-Fig1.png