What is the command to remove a container and reclaim the space?

Solution 1:

The commands are given below.

sudo diskutil apfs deletecontainer disk0s3
sudo diskutil erasevolume free none disk0s3
sudo diskutil apfs resizecontainer disk0s2 0

The first command converts the undesired APFS container partition disk0s3 to a JHFS+ formatted partition. The second command converts the disk0s3 partition to free space. The third command expands the desired APFS container partition disk0s2 to consume the free space.

Below is an example. Your results may be sightly different.

Marlin:~ davidanderson$ diskutil list disk0
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.0 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                 Apple_APFS Container disk2         450.0 GB   disk0s2
   3:                 Apple_APFS Container disk1         49.7 GB    disk0s3
Marlin:~ davidanderson$ sudo diskutil apfs deletecontainer disk0s3
Started APFS operation on disk1
Deleting APFS Container with all of its APFS Volumes
Unmounting Volumes
Unmounting Volume "Recovery" on disk1s1
Deleting Volumes
Deleting Container
Wiping former APFS disks
Switching content types
Reformatting former APFS disks
Initialized /dev/rdisk0s3 as a 46 GB case-insensitive HFS Plus volume with a 8192k journal
Mounting disk
1 new disk created or changed due to APFS operation
Disk from APFS operation: disk0s3
Finished APFS operation on disk1
Marlin:~ davidanderson$ diskutil list disk0
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.0 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                 Apple_APFS Container disk2         450.0 GB   disk0s2
   3:                  Apple_HFS Untitled                49.7 GB    disk0s3
Marlin:~ davidanderson$ sudo diskutil erasevolume free none disk0s3
Started erase on disk0s3 Untitled
Unmounting disk
Finished erase on disk0
Marlin:~ davidanderson$ diskutil list disk0
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.0 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                 Apple_APFS Container disk2         450.0 GB   disk0s2
Marlin:~ davidanderson$ sudo diskutil apfs resizecontainer disk0s2 0
Started APFS operation
Aligning grow delta to 49,790,267,392 bytes and targeting a new physical store size of 499,790,266,368 bytes
Determined the maximum size for the targeted physical store of this APFS Container to be 499,789,236,224 bytes
Resizing APFS Container designated by APFS Container Reference disk2
The specific APFS Physical Store being resized is disk0s2
Verifying storage system
Performing fsck_apfs -n -x /dev/disk0s2
Checking volume
Checking the container superblock
Checking the space manager
Checking the object map
Checking the APFS volume superblock
Checking the object map
Checking the fsroot tree
Checking the snapshot metadata tree
Checking the extent ref tree
Checking the snapshots
Verifying allocated space
The volume /dev/disk0s2 appears to be OK
Storage system check exit code is 0
Growing APFS Physical Store disk0s2 from 449,999,998,976 to 499,790,266,368 bytes
Modifying partition map
Growing APFS data structures
Finished APFS operation
Marlin:~ davidanderson$ diskutil list disk0
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.0 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                 Apple_APFS Container disk2         499.8 GB   disk0s2
Marlin:~ davidanderson$