It’s always satisfying to fix something that seems impossibly broken.  I recently fixed a broken flash drive that could not be reformatted.  I first tried to repair the disk using Disk Utility, but that option was completely greyed out (same thing in the Recovery HD).  So then I tried to erase it but was presented with the error “OS X can’t repair the disk “MYDISK”.”

Next, I tried to partition the disk and received the error message “Disk Erase failed: Disk Erase failed with the error: Unable to write to the last block of the device.”

After some searching, I came across this helpful post.  Here are the steps I used to fix it:

Find The Disk Device File Associated With The Drive

First, you need to find the device file that corresponds with your flash drive.  Run this command:

diskutil list

You should see some entries like /dev/disk0, or /dev/disk1.  Take note of the one that is assigned to your flash drive.  It will be used in the next steps.

Format The Drive Before The System Completely Grabs Onto It

This is the weird part.  You will be using this command (make sure to substitute your device file if it is different) to write random data to the flash drive (which is essentially what reformatting does anyway):

First, become root:

sudo su

Repeatedly run this command (using the up arrow to access it again), while plugging in your flash drive until the command prompt disappears (meaning it is writing to your flash drive).  If you get the resource busy error.  Unplug the drive and try again.  You just have to get the command to run just as the flash drive is plugged in.

cat /dev/random > /dev/disk1

Take a look here for the full instructions and details as they explain everything very well.