Error while unpartitioning hard drive? I've tried to install Ubuntu (and gave up), but now there's a stuck partition, of 24.22 gigabytes.



The pink shows the partition.

I want to unpartition it, but when I try to delete it, an error shows.



Now, I have 24 unusable gigabytes. I've tried running First Aid on the master disk, but I still cannot reset the partition.

Is there a way to fix this?

Edit: If I try it in recovery mode, it says "The target Core Volume is locked."

The results of diskutil list:

/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *121.3 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:          Apple_CoreStorage Macintosh HD            96.1 GB    disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
   4:       Microsoft Basic Data UBUNTU                  24.2 GB    disk0s4
/dev/disk1 (internal, virtual):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                  Apple_HFS Macintosh HD           +95.8 GB    disk1
                                 Logical Volume on disk0s2
                                 32D2E33F-8D67-4B7F-8CF5-ADFCB1C01C74
                                 Unlocked Encrypted
/dev/disk2 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk2
   1:                        EFI EFI                     209.7 MB   disk2s1
   2:                  Apple_HFS Seagate Backup Plus ... 999.9 GB   disk2s2
Boot from Recovery (hold Command ⌘+R on startup), open Disk Utility and retry your partition deletion operation.

Side note, for partitions on the boot drive, it's usually best (if not required) to use Recovery.

EDIT:

To remove the 24.22 GB Ubuntu partition, try the following command after unlocking the CoreStorage group with diskutil cs unlockVolume (backups are always recommended):

diskutil cs revert 32D2E33F-8D67-4B7F-8CF5-ADFCB1C01C74


In theory, the above command should revert the partition "Macintosh HD" to a regular partition (sans CoreStorage and encryption). Then:

diskutil eraseVolume /dev/disk0s4


should remove the Ubuntu partition.

To merge the partitions, open Disk Utility from Recovery, select the ex-Ubuntu partition and click - underneath the pie chart. This should remove the ex-Ubuntu partition and merge it with your Macintosh HD primary partition.