Errors during factory reset on a device using encryption Whenever factory reset is tried on device using encryption, the following error shows up:

wiping data...  
formatting /data...  
E:failed to mount /data (Invalid argument)  
E:format_volume failed to mount "/data"  
E:format_volume: wipe_data_without_wipe_media failed  
Formatting /cache...  
Data wipe failed.  


What is the solution?
Solution is to format /data + /data/media.  

If you ever wanted to do factory reset after using encryption, you need to format the entire /data partition. 

What you have tried is formatting /data without deleting the contents of /data/media. This wouldn't work because this step tries to mount /data partition in recovery and then delete the contents of it without touching /data/media.  

The primary reason for this error is the recovery cannot mount the encrypted /data partition.  

So, its required to backup the contents of /data/media aka Internal Storage before every factory reset (formatting /data + /data/media) on device using encryption.