What is the correct way to eject a APFS dmg from the command line? Create a APFS DMG using Apple's Disk Utility application and call it test_apfs.dmg.

One can mount it with:

hdiutil attach -plist ~/Desktop/test_apfs.dmg


which provides the following output:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
    &lt;key&gt;system-entities&lt;/key&gt;
    &lt;array&gt;
        &lt;dict&gt;
            &lt;key&gt;content-hint&lt;/key&gt;
            &lt;string&gt;GUID_partition_scheme&lt;/string&gt;
            &lt;key&gt;dev-entry&lt;/key&gt;
            &lt;string&gt;/dev/disk2&lt;/string&gt;
            &lt;key&gt;potentially-mountable&lt;/key&gt;
            &lt;false/&gt;
            &lt;key&gt;unmapped-content-hint&lt;/key&gt;
            &lt;string&gt;GUID_partition_scheme&lt;/string&gt;
        &lt;/dict&gt;
        &lt;dict&gt;
            &lt;key&gt;content-hint&lt;/key&gt;
            &lt;string&gt;Apple_APFS&lt;/string&gt;
            &lt;key&gt;dev-entry&lt;/key&gt;
            &lt;string&gt;/dev/disk2s1&lt;/string&gt;
            &lt;key&gt;potentially-mountable&lt;/key&gt;
            &lt;false/&gt;
            &lt;key&gt;unmapped-content-hint&lt;/key&gt;
            &lt;string&gt;7C3457EF-0000-11AA-AA11-00306543ECAC&lt;/string&gt;
        &lt;/dict&gt;
        &lt;dict&gt;
            &lt;key&gt;content-hint&lt;/key&gt;
            &lt;string&gt;41504653-0000-11AA-AA11-00306543ECAC&lt;/string&gt;
            &lt;key&gt;dev-entry&lt;/key&gt;
            &lt;string&gt;/dev/disk3s1&lt;/string&gt;
            &lt;key&gt;mount-point&lt;/key&gt;
            &lt;string&gt;/Volumes/Untitled&lt;/string&gt;
            &lt;key&gt;potentially-mountable&lt;/key&gt;
            &lt;true/&gt;
            &lt;key&gt;unmapped-content-hint&lt;/key&gt;
            &lt;string&gt;41504653-0000-11AA-AA11-00306543ECAC&lt;/string&gt;
            &lt;key&gt;volume-kind&lt;/key&gt;
            &lt;string&gt;apfs&lt;/string&gt;
        &lt;/dict&gt;
        &lt;dict&gt;
            &lt;key&gt;content-hint&lt;/key&gt;
            &lt;string&gt;EF57347C-0000-11AA-AA11-00306543ECAC&lt;/string&gt;
            &lt;key&gt;dev-entry&lt;/key&gt;
            &lt;string&gt;/dev/disk3&lt;/string&gt;
            &lt;key&gt;potentially-mountable&lt;/key&gt;
            &lt;false/&gt;
            &lt;key&gt;unmapped-content-hint&lt;/key&gt;
            &lt;string&gt;EF57347C-0000-11AA-AA11-00306543ECAC&lt;/string&gt;
        &lt;/dict&gt;
    &lt;/array&gt;
&lt;/dict&gt;
&lt;/plist&gt;


After I try to detach with:

hdiutil detach /Volumes/Untitled


diskutil list, returns:

/dev/disk2 (disk image):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        +100.0 MB   disk2
   1:                 Apple_APFS Container disk3         100.0 MB   disk2s1

/dev/disk3 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +100.0 MB   disk3
                                 Physical Store disk2s1
   1:                APFS Volume Untitled                65.5 KB    disk3s1


and going back to Apple's Disk Utility application, I see the Untitled volume listed, but it is grayed out.

I can only really fully eject it if I then do:

hdiutil detach /dev/disk2


So, mounting is one command, fully ejecting is two.

Is this the only / correct way to eject this kind of volume from the command line? What I would like to be able to do is fully eject it with a single command. Is that possible?
Use the following command.

diskutil eject disk2


From the man pages for hdiutil, the following appears:

 detach dev_name [-force]
            detach a disk image and terminate any associated process.
            dev_name is a partial /dev node path (e.g. "disk1").  As of
            Mac OS X 10.4, dev_name can also be a mountpoint.  If Disk
            Arbitration is running, detach will use it to unmount any
            filesystems and detach the image.  If not, detach will attempt
            to unmount any filesystems and detach the image directly
            (using the `eject' ioctl).  If Disk Arbitration is not run-
            ning, it may be necessary to unmount the filesystems with
            umount(8) before detaching the image.  eject is a synonym for
            detach.  In common operation, detach is very similar to
            diskutil(8)'s eject.

            Options:
            -force   ignore open files on mounted volumes, etc.


So the documentation states you may need to unmount filesystems first. I assume this is what the hdiutil detach /Volumes/Untitled command did.

From the man pages for diskutil, the following appears:

 eject device
            Eject a disk.  Media will become offline for the purposes of
            being a data store for file systems or being a member of con-
            structs such as software RAID or direct data.  Additionally,
            removable media will become eligible for safe manual removal;
            automatically-removable media will begin its physical (motor-
            ized) eject sequence.


Here, there is no mention of having to run any other commands first. 


  Note: For the diskutil eject disk2 command to work, the Untitled volume can not be in use.