How to factory recover rooted phone/tablet with encrypted storage?

This procedure is fairly easy, but not obvious to many and so I wanted to capture the steps.

So the problem is that you cannot do a factory reset of the device, nor flash it and install a new ROM because recovery or TWRP is not able to mount the data partition.
To fix thi issue, Install CWM recovery instead. Boot up and go to Recovery, then select option: ‘Mount and Storage page’, and choose “format /data and format /data/media” option at the bottom of the list. This option doesn’t require ClockWorkMod to try mounting the partition, which always fails due to encryption. Instead it just formats it.

After you’ve formatted using above option, either factory reset your device, or install TWRP and flash a new ROM!