SD card won't format

It is probably enough to overwrite existing data with zeros in the first megabyte, where the bootloader and the partition table reside. This is much faster and causes much wear of the memory cells compared to wiping the whole device. After that you can format the card without problem (as long as the card is not damaged physically).

mkusb-dus can do this job automatically. Either wipe the first megabyte or restore the drive to a standard storage device with an MSDOS partition table and a FAT32 file system. See these links,

help.ubuntu.com/community/mkusb

help.ubuntu.com/community/mkusb/wipe

enter image description here