How can I merge primary APFS partition with other empty/free space APFS partition? I am using MacOS Mojave. Below is the diskutil output  



There is 150 GB of free Space. After executing the resize command, I am seeing Error 69519. How do I merge the free space with disk0s2?

MacBook-Pro:~ mac$ sudo diskutil apfs resizeContainer disk0s2 0  
Started APFS operation  
Error: -69519: The target disk is too small for this operation, or a gap is required in your partition
map which is missing or too small, which is often caused by an attempt to grow a partition beyond the
beginning of another partition or beyond the end of partition map usable space  


I have ran the following steps (disk0s4 is not seen in the image as its deleted and is now free space)

Step 1: Delete the container

sudo diskutil apfs deleteContainer disk0s4


Step 2: Erase the volume

sudo diskutil eraseVolume "Free Space" %noformat% /dev/disk0s4


Step 3: Resize the container

sudo diskutil apfs resizeContainer disk0s2 0
You need to know the partitioning of the drive. In particular, were the free space is. The diskutil list disk0 command shows the partitions in ascending order and the size of each partition. However, this command does not show free space. The Disk Utility application does occasionally show free space but the value is often incorrect and does not show where the free space is with respect to the other partitions

So where is the free space shown? Here are a few ways to determine where the free space is.


The sudo gpt -r show /dev/disk0 command will show the logical sector blocks used by each partition. The command will also show the free space between each partition in logical sector block units. In the days of OS X, you could run this command on the boot drives. Starting with macOS, System Integrity Protection (SIP) has to be disabled to do this.
Use the diskutil info -plist disk0s# command to retrieve partition number # start and size byte values. You need to repeat the command for each partition on a drive. Next, you can compile a table similar to the output from the gpt -r show /dev/disk0 command. Basically, free space is any space not allocated to a partition, the Protected Master Boot Record (PMBR) and the GUID Partition Tables (GPT).
Write a BASH script that would automate way number 2.  A free script can be downloaded from this site. Assuming your downloads goto to the default Downloads folder, you can get the free space by entering the command shown below. Note: You do not need to enter sudo or disable SIP. 

~/Downloads/driveinfo-1.0.1/driveinfo -r disk0




  In order to add free space to a APFS container partition, the free space must reside directly below the partition.


When you deleted disk0s4, you created free space directly after disk0s3. Since disk0s3 is between disk0s2 and this free space, you can not recover the free space by using the command given in your Step 3. You could recover the free space into disk0s3 by entering the command given below.

sudo diskutil resizeVolume disk0s3 R