PowerCLI

 View Only
  • 1.  Clear VSAN partitions with PowerCLI

    Posted May 13, 2023 06:31 PM

    Common task for R&D Lab. I have moderately large VSAN cluster with large number of disks previously participated in VSAN.
    vCenter task "Erase all partitions" fails
    The only way to clean is to ssh to all ESXi hosts and "esxcli vsan strorage remove"

    Current status: I can get all GroupUUID by host and generate .sh files for each host with corresponding "esxcli vsan storage remove --uuid 521a2882-0d7f-ef06-a117-d436718c6248"

    It makes life easier, but still a lot of manual work for 10+ hosts cluster.

    Get-ESXCli provides vsan.storage abstraction with remove method, but with unclear documentation and no examples.

    Please point me to right direction - how can I clear VSAN partitions 100% automated?



  • 2.  RE: Clear VSAN partitions with PowerCLI
    Best Answer

    Posted May 13, 2023 07:21 PM

    When you use the V2 switch, help is available

    $esxcli = Get-EsxCli -VMHost <MyEsx> -V2
    $esxcli.vsan.storage.remove.Help()

    You can create the hash table, fill in the required parameter, and then Invoke the method.

    $remVSAN = $esxcli.vsan.storage.remove.CreateArgs()
    $remVSAN['uuid'] = <a groupUuid>
    $esxcli.vsan.storage.remove.Invoke($remVSAN)

    You can loop over all ESXi nodes and then all UUIDs



  • 3.  RE: Clear VSAN partitions with PowerCLI

    Posted May 14, 2023 01:34 AM

    Great help, may thanks.

    Working code for reference:

    foreach ($vmhost in $vmhosts) {
      $esxcli = Get-EsxCli -VMHost $vmhost -V2
    
      [String[]]$diskGroups = @()
    
      $diskgroups += $esxcli.vsan.storage.list.Invoke() | findstr "pUUID"
      [String[]]$uniqueDiskGroups = $diskGroups | Sort-Object -Unique
    
      Write-Host 'Processing host' $vmhost.Name
      Write-Host ' - Disk groups found '
    
      $uniqueDiskGroups
    
      Write-Host ' -'
    
      foreach ($groupUUID in $uniqueDiskGroups) {
        $UUID = $groupUUID.Substring(31, 36)
        Write-Host 'Removing '$UUID
        $remVSAN = $esxcli.vsan.storage.remove.CreateArgs.Invoke()
        $remVSAN.uuid = $UUID;
        $esxcli.vsan.storage.remove.Invoke($remVSAN)
      }
    
      Write-Host ' -'
      Write-Host ' '
    }


  • 4.  RE: Clear VSAN partitions with PowerCLI

    Posted May 14, 2023 05:02 AM

    I think you have a typo in 1 line.
    This

    $remVSAN = $esxcli.vsan.storage.remove.CreateArgs.Invoke()

    should be

    $remVSAN = $esxcli.vsan.storage.remove.CreateArgs()


  • 5.  RE: Clear VSAN partitions with PowerCLI

    Posted May 14, 2023 05:03 AM

    Well, it worked with .Invoke()

     



  • 6.  RE: Clear VSAN partitions with PowerCLI

    Posted May 14, 2023 05:12 AM

    Strange, that seems to work indeed.
    One learns something new every day 



  • 7.  RE: Clear VSAN partitions with PowerCLI

    Posted Nov 03, 2025 11:45 AM

    Hello guys, 

    I am trying to delete old vSAN OSA partition using your script above. But get this error:

    Following vSAN disk groups found : 
    VSANDiskGroupUUID            : 52832df6-2448-1288-ee80-7471a45e3d22
    VSANDiskGroupUUID            : 52dadad5-6ad4-a3ae-b904-c2ca57af05bb
    Start removing disk groups with UUID: 52832df6-2448-1288-ee80-7471a45e3d22  ?
    Press Enter to continue...:
    Unable to remove device: Disk mapping is not mounted on this host: t10.NVMe____Micron_7450_MTFDKCB3T2TFS_______________031D8A480175A000
    At \GIT\vcf\Host\Clear-vSAN-Partitions-on-ESX.ps1:28 char:5
    +     $esxcli.vsan.storage.remove.Invoke($remVSAN)
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], ViError
        + FullyQualifiedErrorId : VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViError

    I have tried to do it directly on host:

    [root@en02-esx014:~] esxcli vsan storage remove -s t10.NVMe____Micron_7450_MTFDKCB3T2TFS_______________B3AD20420175A000
    Unable to remove device: Disk mapping is not mounted on this host: t10.NVMe____Micron_7450_MTFDKCB3T2TFS_______________B3AD20420175A000

    [root@en02-esx:~] esxcli vsan storage automode set --enabled false
    [root@en02-esx:~] esxcli vsan storage diskgroup mount -s t10.NVMe____Micron_7450_MTFDKCB3T2TFS_______________B3AD20420175A000
    Unable to mount: Failed to load dek for encrypted disk on encryption off host

    Is the ecryption on vSAN the reason for this issue?

    Do you have any hints for me?

    Thank you in advance!

    Regards, 

    Cop

    -------------------------------------------



  • 8.  RE: Clear VSAN partitions with PowerCLI

    Posted Jan 28, 2026 10:59 AM

    Update:

    Finally for me, is the easiest way to delete vSAN disk groups BEFORE the esx host will be removed from the cluster.

    If it was not correctly completed, I use SSH and kill vSAN partitions for my MICRON NVMEs so:

    1) Check your SSDs: 

    vdq -qH | egrep -o -B2 'Ineligible' | grep 'Name:' | grep 'Micron_7450'

    2) Kill vSAN partition #2:

    for x in $(vdq -qH | egrep -o -B2 'Ineligible' | grep 'Name:' | grep 'Micron_7450' | awk '{print $NF}'); do partedUtil delete /vmfs/devices/disks/$x 2; done

    3) Kill vSAN partition #1:

    for x in $(vdq -qH | egrep -o -B2 'Ineligible' | grep 'Name:' | grep 'Micron_7450' | awk '{print $NF}'); do partedUtil delete /vmfs/devices/disks/$x 1; done

    Maybe it helps someone...

    -------------------------------------------