How to remove derelict or broken VMs from ESXi via CLI

I recently ran into an issue with one of my SSDs failing in my ESXi v6.7 NUC. All of my client VMs were house on this, now dead, datastore so all VMs were showing in vSphere Web as “Invalid.” One of the unfortunate side-effects of this error is the complete inability to really do anything to the VM – you can’t edit settings, you can’t unregister, and you definitely can’t delete.

Luckily, it is possible to delete these abominations. Unluckily, it requires some command-line magic.

The first step is to enable SSH on the ESXi host. No reboot is needed to enable SSH.

Once SSH is enabled, access the ESXi host and login.

vim-cmd /vmsvc/getallvms

Will list all VMs either currently or previously registered to this particular ESXi instance. I don’t remember the exact verbiage but it reported something to the effect of:

Skipping invalid VM 1

This error repeated for each VM that had been taken along with the now defunct SSD. Once the list of failed VMs is shown, run the following command for each VM:

vim-cmd /vmsvc/unregister <id>

And that’s it. All ghosts of the former VMs should be removed and your ESXi WebUI should be clear of errored clients.