By William Lam, Sr. Technical Marketing Engineer

Prior to vSphere 5, unpresenting a LUN from a set of ESX(i) hosts was not the easiest of tasks, as it took several steps that included masking rules on an ESX(i) host before and after the removal of a LUN (see this KB for more information). In vSphere 5, this process has been simplified by introducing an “unmount” datastore operation and “detach” storage device operation in the vSphere Client.

The unmount operation is a single step, but there is a caveat when an administrator detaches the LUN which is required prior to unpresenting it from the storage array. The administrator must go to each host that the LUN is attached to and detach it. This is trivial for one or two hosts, but can be quite tedious across several dozen hosts or having more than one LUN to unpresent.

Administrators also have the option of performing the unmount and detach operation using the command line via esxcli, but the commands are still required to be executed on each and every host the LUN is attached to.

How do we make this easier for administrators with lots of hosts? We can leverage the power of the vSphere API to connect to the vCenter Server, select a datastore and perform an unmount and/or detach operation across all ESX(i) hosts.

Below we have a PowerCLI and vSphere SDK for Perl script that allows a user to connect either to a vCenter Server and/or directly to an ESX(i) host to easily perform a “unmount” and “detach” operation for unpresenting a LUN.

Disclaimer: This script is provided for informational/educational purposes only. It should be thoroughly tested before attempting to use in a production environment.


Download script: DatastoreFunctions.ps1

Usage: To run this script you will need the latest version of PowerCLI installed and PowerShell v2.

List all datastores and their mount/detach status by passing your datastore object(s) into the Get-DatastoreMountInfo advanced function as shown below:

To unmount a datastore pass your datastore object(s) to the Unmount-Datastore advanced function as seen below, this will unmount the datastore from all attached hosts:


Once unmounted you will then be able to detach the datastore from the hosts using the Detach-Datastore cmdlet as shown below:

vSphere SDK for Perl

Download script:

Usage: To run the script you will need to have VMware vCLI installed on either a Windows/Linux system or you can use the VMware vMA appliance. The script will prompt users to confirm both “unmount” and “detach” operation before proceeding.

List all datastores and their mount/detach status using the “list” operation:


To unmount a datastore, specify the datastore using –datastore and “unmount” operation:


To verify the “unmount” operation was successful, we will re-run the “list” operation and look for the “UNMOUNTED” string:


To detach the LUN, specify the datastore using –datastore and “detach” operation:


To verify that the detach operation was successfully, we will re-run the “list” operation again and look for the “DETACHED” string:


Get notification of new blog postings and more by following lamw on Twitter:  @lamw

About the Author

William Lam

William Lam works as a Staff Engineer in VMware R&D and focuses Automation, Integration and Operations. William also runs a personal blog at Previous to VMware, he was a Systems Engineer managing large vSphere installations and UNIX/Linux systems.