Manually Remove Hyper-V Host Cluster from SCVMM 2012 Database

Hello,

It’s been a long time, but not always does one create something that is worth sharing 🙂

I have been playing with System Center Virtual Machine Manager 2012 since the Beta was released, and I love it.

There were however some problems which are still inherited from the 2008 R2 version.

One problem was failure to remove a host, or a host cluster from the SCVMM database.

Unlike the 2008 R2 version, I searched around and i found no database scripts available which can help me clean the SCVMM 2012 database, so i decided to create my own.

Here is the explanation and some stuff you should know.

My Setup:

  • SCVMM 2012 RC
  • VMM Database is deployed to SQL Server 2008 R2
  • Running Hyper-V Cluster on 4 hosts Windows Server 2008 R2 Enterprise
  • EVA 4400 SAN Storage

How the script works:

Basically the script runs through the VMM Database, deleting all entries of the hosts and any data related to them.

The script also checks to see whether this host is the last host in the cluster, if this host is indeed the last one, it will also remove the cluster and all its related data.

The script however does not do the following:

  • Delete any Host Groups or Host Group settings
  • Delete any logical networks or logical network definitions

This is basically because you can manually delete them using the VMM console once it is up and running again.

How to use the script:

  1. Well the first step is always to back up your existing state, even if it is not working properly, so go ahead and back up your VMM Database before you start 🙂
  2. Disable the “System Center Virtual Machine Manager” Service on you VMM Server (make sure you disable it on all VMM Servers in case you have a highly available setup)
  3. Login to you SQL Server Database Server, and Start SQL Server Management Studio
  4. Click the “New Query” button, and paste the script below
  5. Replace the <Placeholders> at the start of the script according to your setup (Only the VMM Database Name and the GUID for the host you want to delete.
  6. In case you do not have the GUID for your host, you can get it by expanding your VMM Database and looking at the table “dbo.tbl_ADHC_Hosts”
  7. After you run the script, go ahead and restart your System Center Virtual Machine Manager Service

The Script:

USE ;

DECLARE @DeleteHostId GUID;
SET @DeleteHostId = ''

PRINT N'Deleting host with GUID ' + RTRIM(CAST(@DeleteHostID AS nvarchar(50)))

PRINT N'Getting host cluster GUID'

DECLARE @HostClusterID GUID;
SET @HostClusterID =
(
SELECT HostClusterID FROM [dbo].[tbl_ADHC_Host]
WHERE HostID = @DeleteHostId
)

IF (@HostClusterID IS NOT NULL)
PRINT N'Retreived host cluster GUID ' + RTRIM(CAST(@HostClusterID AS nvarchar(50)))
ELSE
PRINT N'This host does not belong to a cluster'

PRINT N'Deleteing physical objects'

DELETE FROM [dbo].[tbl_WLC_PhysicalObject]
WHERE HostId = @DeleteHostId

PRINT N'Deleteing virtual objects'

DELETE FROM [dbo].[tbl_WLC_VObject]
WHERE HostId = @DeleteHostId

PRINT N'Prepairing to delete host network adapters'

DECLARE @HostNetworkAdapterCursor CURSOR;
DECLARE @HostNetworkAdapterID GUID;
SET @HostNetworkAdapterCursor = CURSOR FOR
(SELECT NetworkAdapterID FROM [dbo].[tbl_ADHC_HostNetworkAdapter])

OPEN @HostNetworkAdapterCursor

FETCH NEXT FROM @HostNetworkAdapterCursor INTO @HostNetworkAdapterID

WHILE (@@FETCH_STATUS = 0)
BEGIN
PRINT N'Prepairing to delete host network adapter with GUID ' + RTRIM(CAST(@HostNetworkAdapterID AS nvarchar(50)))

PRINT N'Deleting logical network mapping for host network adapter with GUID ' + RTRIM(CAST(@HostNetworkAdapterID AS nvarchar(50)))

DELETE FROM [dbo].[tbl_NetMan_HostNetworkAdapterToLogicalNetwork]
WHERE HostNetworkAdapterID = @HostNetworkAdapterID

PRINT N'Deleting IP subnet VLAN mapping for host network adapter with GUID ' + RTRIM(CAST(@HostNetworkAdapterID AS nvarchar(50)))

DELETE FROM [dbo].[tbl_NetMan_HostNetworkAdapterToIPSubnetVLan]
WHERE HostNetworkAdapterID = @HostNetworkAdapterID

FETCH NEXT FROM @HostNetworkAdapterCursor INTO @HostNetworkAdapterID
END

CLOSE @HostNetworkAdapterCursor
DEALLOCATE @HostNetworkAdapterCursor

PRINT N'Completing host network adapters deletion'

DELETE FROM [dbo].[tbl_ADHC_HostNetworkAdapter]
WHERE HostID = @DeleteHostId

PRINT N'Deleting virtual networks'

DELETE FROM [dbo].[tbl_ADHC_VirtualNetwork]
WHERE HostID = @DeleteHostId

PRINT N’Deleting virtual switch extensions’

DELETE FROM [dbo].[tbl_NetMan_InstalledVirtualSwitchExtension]
WHERE HostID = @DeleteHostId

PRINT N'Deleting host volumes'

DELETE FROM [dbo].[tbl_ADHC_HostVolume]
WHERE HostID = @DeleteHostId

PRINT N’Deleting pass through disks’

DELETE FROM [dbo].[tbl_WLC_VDrive]
WHERE HostDiskId IN (SELECT DiskID FROM [dbo].[tbl_ADHC_HostDisk] WHERE HostID IN (SELECT HostID FROM [dbo].[tbl_ADHC_Host] WHERE HostID = @DeleteHostId))

PRINT N'Deleting host disks'

DELETE FROM [dbo].[tbl_ADHC_HostDisk]
WHERE HostID = @DeleteHostId

PRINT N'Prepairing to delete host bus adapters'

DECLARE @HostBusAdapterCursor CURSOR;
DECLARE @HostBusAdapterID GUID;
SET @HostBusAdapterCursor = CURSOR FOR
(SELECT HbaID FROM [dbo].[tbl_ADHC_HostBusAdapter])

OPEN @HostBusAdapterCursor

FETCH NEXT FROM @HostBusAdapterCursor INTO @HostBusAdapterID

WHILE (@@FETCH_STATUS = 0)
BEGIN

PRINT N'Prepairing to delete host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))

PRINT N'Deleting fiber port mapping for host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))

DECLARE @FiberPortID GUID;
SET @FiberPortID =
(
SELECT PortID FROM [dbo].[tbl_ADHC_FCHbaToFibrePortMapping]
WHERE FCHbaID = @HostBusAdapterID
)

DELETE FROM [dbo].[tbl_ADHC_FCHbaToFibrePortMapping]
WHERE FCHbaID = @HostBusAdapterID

PRINT N'Deleting fiber port with GUID ' + RTRIM(CAST(@FiberPortID AS nvarchar(50)))

DELETE FROM [dbo].[tbl_ADHC_FibrePort]
WHERE PortID = @FiberPortID

PRINT N'Deleting fiber channel mapping for host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))

DELETE FROM [dbo].[tbl_ADHC_HostFibreChannelHba]
WHERE FCHbaID = @HostBusAdapterID

PRINT N'Deleting any iSCSI entries for host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))

DECLARE @iSCSITargets TABLE
(
TargetID GUID
)
INSERT INTO @iSCSITargets (TargetID)
SELECT TargetID FROM [dbo].[tbl_ADHC_ISCSIHbaToTargetMapping]
WHERE ISCSIHbaID = @HostBusAdapterID

PRINT N'Deleting iSCSI host bus adapter to target mapping for mapping for host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))

DELETE FROM [dbo].[tbl_ADHC_ISCSIHbaToTargetMapping]
WHERE ISCSIHbaID = @HostBusAdapterID

PRINT N'Deleting iSCSI host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))

DELETE FROM [dbo].[tbl_ADHC_HostInternetSCSIHba]
WHERE ISCSIHbaID = @HostBusAdapterID

PRINT N'Deleting iSCSI targets for host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))

DECLARE @iSCSITargetIDCursor CURSOR;
DECLARE @iSCSITargetID GUID;
SET @iSCSITargetIDCursor = CURSOR FOR
(SELECT TargetID FROM @iSCSITargets)

OPEN @iSCSITargetIDCursor

FETCH NEXT FROM @iSCSITargetIDCursor INTO @iSCSITargetID

WHILE (@@FETCH_STATUS = 0)
BEGIN

PRINT N'Deleting iSCSI targets with GUID ' + RTRIM(CAST(@iSCSITargetID AS nvarchar(50)))

DELETE FROM [dbo].[tbl_ADHC_ISCSITarget]
WHERE TargetID = @iSCSITargetID

FETCH NEXT FROM @iSCSITargetIDCursor INTO @iSCSITargetID
END

CLOSE @iSCSITargetIDCursor
DEALLOCATE @iSCSITargetIDCursor

FETCH NEXT FROM @HostBusAdapterCursor INTO @HostBusAdapterID
END

CLOSE @HostBusAdapterCursor
DEALLOCATE @HostBusAdapterCursor

PRINT N'Completing host bus adapters deletion'

DELETE FROM [dbo].[tbl_ADHC_HostBusAdapter]
WHERE HostID = @DeleteHostId

PRINT N'Prepairing to delete agent servers'

DECLARE @AgentServerID  GUID;
SET @AgentServerID =
(
SELECT AgentServerID FROM [dbo].[tbl_ADHC_AgentServerRelation]
WHERE HostLibraryServerID = @DeleteHostID
)

PRINT N'Deleting agent server relations'

DELETE FROM [dbo].[tbl_ADHC_AgentServerRelation]
WHERE HostLibraryServerID = @DeleteHostID

PRINT N'Deleting health monitor data for agent server with GUID ' + RTRIM(CAST(@AgentServerID AS nvarchar(50)))

DELETE FROM [dbo].[tbl_ADHC_HealthMonitor]
WHERE AgentServerID = @AgentServerID

PRINT N'Deleting agent server with GUID ' + RTRIM(CAST(@AgentServerID AS nvarchar(50)))

DELETE FROM [dbo].[tbl_ADHC_AgentServer]
WHERE AgentServerID = @AgentServerID

PRINT N'Deleting host GPUs'

DELETE FROM [dbo].[tbl_ADHC_HostGPU]
WHERE HostID = @DeleteHostId

PRINT N'Deleting host'

DELETE FROM [dbo].[tbl_ADHC_Host]
WHERE HostID = @DeleteHostId

IF (@HostClusterID IS NOT NULL)
BEGIN

PRINT N'Checking to see if any other hosts are joined to the same cluster'

DECLARE @HostCount INT;
SET @HostCount =
(
SELECT COUNT(*) FROM [dbo].[tbl_ADHC_Host]
WHERE HostClusterID = @HostClusterID
)

PRINT N'There are ' + RTRIM(CAST(@HostCount AS nvarchar(50))) + N' currently joined to the same cluster'

IF (@HostCount = 0)
BEGIN

PRINT N'Deleting cluster disks'

DELETE FROM [dbo].[tbl_ADHC_ClusterDisk]
WHERE ClusterID = @HostClusterID

PRINT N'Deleting cluster'

DELETE FROM [dbo].[tbl_ADHC_HostCluster]
WHERE ClusterID = @HostClusterID
END
ELSE
PRINT N'This host is not the last host in the cluster, the cluster will be deleted upon the deletion of the last host.'
END
ELSE
PRINT N'This host does not belong to a cluster, no clusters will be deleted'

GO

And that’s it 🙂

I have tried this script a couple of times and it has working well for me.

But please do not hesitate to leave a comment in case there is anything that’s not working for you.

Edit: Thanks to Yusuf Ozturk the script now also supports deleting Pass-through Disks and Virtual Switch Extensions in case they were used by the hosts.

27 comments

    1. Hey jimmy, you do not need to deal with the VMM Database in order to delete VMs, you can always use PowerShell: Get-VM -Name “VMName” | Repair-VM -Force
      if it did not work just try to migrate the VMs between hosts to see what the problem maybe.

  1. Thx much for this.

    I tried to commit the query but get several errors from deleting iSCSI Targets, one of them pasted below.

    Msg 547, Level 16, State 0, Line 158
    The DELETE statement conflicted with the REFERENCE constraint “FK_tbl_ADHC_ISCSIHbaToTargetMapping_tbl_ADHC_ISCSITarget”. The conflict occurred in database “VirtualManagerDB”, table “dbo.tbl_ADHC_ISCSIHbaToTargetMapping”, column ‘TargetID’.

    In other aspects it seems to do the trick 🙂
    I won´t commit it though until I know it´s safe as this VMM server also handles the new cluster and I must be Confident that it won´t be affected.

    Do you have any idea how to fix it ?

  2. I forgot to mention that the SAN the old cluster used is dead and gone by now, but as I see this in the error rows I dare not to commit it.

    Deleting iSCSI targets with GUID 87567411-85E6-4D9A-9956-7387551241A5
    Msg 547, Level 16, State 0, Line 158
    The DELETE statement conflicted with the REFERENCE constraint “FK_tbl_ADHC_ISCSIHbaToTargetMapping_tbl_ADHC_ISCSITarget”. The conflict occurred in database “VirtualManagerDB”, table “dbo.tbl_ADHC_ISCSIHbaToTargetMapping”, column ‘TargetID’.
    The statement has been terminated.

    (1 row(s) affected)
    Deleting iSCSI targets with GUID 87567411-85E6-4D9A-9956-7387551241A5

    That GUID 875…
    Is an iSCSI target on the new SAN so I don´t wanna jeopardise that anything happens to the new cluster in VMM, but I suppose that it only deletes any possible links and connections to the old cluster and thus not affecting the new cluster at all, so i can disregard it and run the query even with the errors ?

    1. @fahlis it seems that you were right, I had no iSCSI targets in my setup, that’s why I never faced the same problem, you can always just backup your database (even if it is not working) and run the script anyway… it will most probably work fine but then you will have to go to the tbl_ADHC_ISCSIHbaToTargetMapping table and delete the conflicting rows yourself.

    1. I have used it with SCVMM 2012 SP1, it worked for me, but again, I must be very clear, backup your database before starting. in case of any problems, you can always restore harmlessly.

  3. Just wanted to chime in to say that this worked out pretty well for me on SCVMM 2012 R2.

    One thing to point out, is that it appears your script doesn’t reference ‘HostSASHba’ and its relevant tables at all. This may be a new construct in SCVMM 2012 R2.

    I had a lot of manual digging and deleting, but your script kept me on track to getting it done!

    Readded the cluster and everything is going great again.

    1. Hi Les,

      I know this is over a year old, but can you let me know how you resolved the HostSASHba errors that popped up?

      I’m not good with SQL at all, so I’m not sure how to resolve the REFERENCE conflict.

      Thanks!

  4. Hi Hazem,

    This is great. I’ve also added a few lines for Physical Passthrough disks and Virtual Switch Extensions. If someone uses Passthough disk and Virtual Switch Extension, please also add:

    PRINT N’Deleting virtual switch extensions’

    DELETE FROM [dbo].[tbl_NetMan_InstalledVirtualSwitchExtension]
    WHERE HostID = @DeleteHostId

    Also before “Deleting host disks” line, add:

    PRINT N’Deleting pass through disks’

    DELETE FROM [dbo].[tbl_WLC_VDrive]
    WHERE HostDiskId IN (SELECT DiskID FROM [dbo].[tbl_ADHC_HostDisk] WHERE HostID IN (SELECT HostID FROM [dbo].[tbl_ADHC_Host] WHERE HostID = @DeleteHostId))

    Regards,

    Yusuf

  5. Hm, in my case with VMM 2012 SP1 it causes VMM to crash at startup… restored DB alredy – fixed startup, but hosts still here and pending…

  6. I was getting an error when running the script related to iSCSI. As a result, the host was not being removed.

    I manually removed the iSCSI references that I think it had an issue with (using the error results in the SQL execution as a guide):
    Used tbl_ADHC_Host to get host GUID
    Used this GUID to find offending HBAID in ‘tbl_ADHC_HostBusAdapter’
    Used the HBAID to manually DELETE the iSCSI iqn address row from ‘tbl_ADHC_HostInternetSCSIHba’
    Used the HBAID to manually DELETE the 8 paths (rows) from ‘tbl_ADHC_ISCSIHbaToPortalMapping’ (I have 8 redundant iSCSI paths to storage)

    That seemed to clear up the iSCSI references manually. Then ran your script, which now successfully cleared the pending host.
    I did get further errors about iSCSI but these were related to another host and I didnt want to remove them… yet. So this worked fine for the one host.

    After adding the Host, I then got:
    Error (21201)
    Another machine with the same SMBIOSGUID is found.
    If I was going to sort this I’d have:

    Used tbl_ADHC_Host to identify PhysicalMachineID (as the umpteenth column along next to the host in question)
    Used PhysicalMachineID to manually DELETE the row it referred to in tbl_PMM_PhysicalMachine

    However, the SCVMM actually requested a reboot to sort iSCSI claim. I just did that and things seem okay.

  7. In 2016 i had to also search for and remove

    search for PMM physicalid

    ADHC_networkhostadapter delete any references by physicalid

    ST_StorageFileServerNode delete the server node by name

  8. Hello Hazem,
    i have used this script multiple times and its worked for me since 2012 sp1 to even 2019.
    But this time i have come across something which is bugging me.
    i removed the entire cluster but one host has gotten stuck. its gives below error with.

    The DELETE statement conflicted with the REFERENCE constraint “FK_tbl_WLC_PhysicalObject_tbl_ADHC_HostVolume”. The conflict occurred in database “VirtualManagerDB”, table “dbo.tbl_WLC_PhysicalObject”, column ‘HostVolumeId’
    when i search the two Tables in question cannot find any HostVolumeID from ADHC_Hostvolume Table in tbl_WLC_PhysicalObject.

    i saw “fahlis” post on Technet which was referenceing some Network Related tables. In my case its some Cluster Shared volume which are getting stuck.
    would really appreciate any help

      1. Hi Guys, unfortunately the last time I’ve used SCVMM was about 7 years ago. I’ve professionally moved on to other areas of tech since. Which is why I can’t help in such case. If someone have figured out any updates to make this script work on more recent versions of SCVMM I’d be happy to link to their update here.

Leave a reply to Hazem Elshabini Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.