Issues erasing APFS disk

disk-utilityhard drivehddinstall

The MacBook Pro (Mid 2012) won't boot because it can't find the system software (flashing questionmark folder when trying to boot).
I tried to reinstall the system by installing macOS on an external HDD, booting the MacBook Pro from the HDD and trying to erase the internal HDD (APFS) which won't work because of the following error:

POSIX reports: The operation couldn't be completed. Input/output error.

I also tried to erase the internal HDD with diskutil apfs deleteContainer /dev/disk2s2, same problem,

Error: 5: Input/output error

Trying to run First Aid gives the following error on the APFS Physical Storage:

The volume /dev/disk0s2 could not be verified completely.
Storage
system check exit code is 8.
Storage system verify or repair failed.
Operation failed…

Anything else I could try?

Best Answer

To reinitialize the drive, you can enter the following. If this does not work, then you should consider that the drive, cable and/or controller may have failed.

Enter the following command.

sudo diskutil unmountdisk disk0

If disk0 does not currently have any partitions, then you will receive the following message, which can be ignored.

Unmount of disk0 failed: at least one volume could not be unmounted

Next, enter the following commands.

sudo gpt destroy disk0
sudo gpt create -f disk0
sudo diskutil partitiondisk disk0 1 gpt apfs "Macintosh HD" R