How to remove old bootcamp partition in terminal

I needed to remove my old windows bootcamp partition, which took up 50GB. Bootcamp itself would not let me do this, as it kept requiring me to have 40GB of storage, which I could not make (my mac is at full capacity, due to the 50GB empty windows partition). I do not understand why i need 40gb of space in order to free up space, but regardless, I ended up doing the (risky?) brute force removal of the partition through disk utility. (I erased the partition contents, and then converted it to an APFS container, since i thought that would let me merge them together, but it did not. Right now, im not really sure whats going on, as disk utility shows i have a "APFS container" with 50GB?

Here is the print out when running diskutil list in terminal:

/dev/disk0 (internal):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                         251.0 GB   disk0
   1:                        EFI EFI                     314.6 MB   disk0s1
   2:                 Apple_APFS Container disk1         200.0 GB   disk0s2
   3:                 Apple_APFS Container disk2         49.9 GB    disk0s3

/dev/disk1 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +200.0 GB   disk1
                                 Physical Store disk0s2
   1:                APFS Volume Macintosh HD            192.0 GB   disk1s1
   2:                APFS Volume Preboot                 20.4 MB    disk1s2
   3:                APFS Volume Recovery                517.8 MB   disk1s3
   4:                APFS Volume VM                      2.1 GB     disk1s4

/dev/disk2 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +49.9 GB    disk2
                                 Physical Store disk0s3

How can I merge disk0s3 with disk0s2 so I can have only 1 disk partition with the full 250GB storage capacity of my SSD?


Solution 1:

To convert a container to free space requires two commands. For example, disk0s3 is a APFS container. To convert to free space, you would need to enter the following commands.

sudo diskutil apfs deleteContainer disk0s3
sudo diskutil eraseVolume free none disk0s3

Free space can be added to a container, if the free space reside immediately after the container. In your case, if you converted disk0s3 to free space, then you probably would be able add this free space to disk0s2, by entering the command given below.

sudo diskutil apfs resizeContainer disk0s2 0