Change the size of a virtual machine

Applies to: ✔️ Linux VMs ✔️ Windows VMs ✔️ Flexible scale sets

This article shows you how to change an existing virtual machine's VM size.

After you create a virtual machine (VM), you can scale the VM up or down by changing the VM size. In some cases, you must deallocate the VM first. Deallocation may be necessary if the new size isn't available on the same hardware cluster that is currently hosting the VM. It is important to understand that even when deallocation is not necessary, if the virtual machine is currently running, changing its size will cause it to restart. For this reason you should consider changing VM size as a disruptive procedure, especially for stateful workloads that are hosted on the VM.

Warning

If the virtual machine is currently running, changing its size will cause it to restart.

Deallocating the VM also releases any dynamic IP addresses assigned to the VM. The OS and data disks are not affected.

If a resize operation fails, the VM model will still display the requested size, but the VM will continue running on its previous size until the resize is successfully allocated. This behavior applies to all GET operations, as the displayed size always reflects the latest applied configuration, not the actual running size.

Choose the right SKU

When resizing a VM, it's important to choose the right SKU based on the signals from the VM to determine whether you need more CPU, memory, or storage capacity:

  • If the VM is running a CPU-intensive workload, such as a database server or a web server with high traffic, you may need to choose a SKU with more CPU cores. For more information, see Compute optimized sizes.
  • If the VM is running a memory-intensive workload, such as a machine learning model or a big data application, you may need to choose a SKU with more memory. For more information, see Memory optimized sizes.
  • If the VM is running out of storage capacity, you may need to choose a SKU with more storage. For more information, see Storage optimized sizes.
  • If your VM uses Premium Storage, make sure that you choose an s version of the size to get Premium Storage support. For example, choose Standard_E4s_v3 instead of Standard_E4_v3.

For more information on choosing the right SKU, you can use the following resources:

Limitations

  1. You can't resize a VM size that has a local temp disk to a VM size with no local temp disk and vice versa.

    The only combinations allowed for resizing are:

  • VM (with local temp disk) -> VM (with local temp disk); and

  • VM (with no local temp disk) -> VM (with no local temp disk).

    For a work-around, see How do I migrate from a VM size with local temp disk to a VM size with no local temp disk? . The work-around can be used to resize a VM with no local temp disk to VM with a local temp disk. You create a snapshot of the VM with no local temp disk > create a disk from the snapshot > create VM from the disk with appropriate VM size that supports VMs with a local temp disk.

  1. You can't resize a VM size that has a SCSI-based VM to a VM size that has a remote NVMe-enabled VM.

Change the VM size

Select an option for changing the VM size:

To change the size of a VM using PowerShell:

  1. Set the resource group and VM name variables. Replace the values with information of the VM you want to resize.

    $resourceGroup = "myResourceGroup"
    $vmName = "myVM"
    
  2. List the VM sizes that are available on the hardware cluster where the VM is hosted.

    Get-AzVMSize -ResourceGroupName $resourceGroup -VMName $vmName
    
  3. Resize the VM to the new size.

    $vm = Get-AzVM -ResourceGroupName $resourceGroup -VMName $vmName
    $vm.HardwareProfile.VmSize = "<newVMsize>"
    Update-AzVM -VM $vm -ResourceGroupName $resourceGroup
    

Use PowerShell to resize a VM not in an availability set.

Note

The local PowerShell may require the VM to restart to take effect.

# Import the Azure module
Import-Module Az
# Login to your Azure account
Connect-AzAccount -Environment AzureChinaCloud
# Set variables
$resourceGroup = 'myResourceGroup'
$vmName = 'myVM'
$size = 'Standard_DS3_v2'
# Select the subscription
Select-AzSubscription -SubscriptionId '<subscriptionID>'
# Get the VM
$vm = Get-AzVM -ResourceGroupName $resourceGroup -Name $vmName
# Change the VM size
$vm.HardwareProfile.VmSize = $size
# Update the VM
Update-AzVM -ResourceGroupName $resourceGroup -VM $vm

Use PowerShell to resize a VM in an availability set

If the new size for a VM in an availability set isn't available on the hardware cluster currently hosting the VM, then you need to deallocate all VMs in the availability set to resize the VM. You also might need to update the size of other VMs in the availability set after one VM has been resized. To resize a VM in an availability set, run the below script.

This script sets the variables $resourceGroup, $vmName, $newVmSize, and $availabilitySetName. It then checks if the desired VM size is available by using Get-AzVMSize and checking if the output contains the desired size. If the desired size isn't available, the script deallocates all VMs in the availability set, resizes them, and starts them again. If the desired size is available, the script resizes the VM.

Replace the values of $resourceGroup, $vmName, $newVmSize, and $availabilitySetName with your own.

# Set variables
$resourceGroup = "myResourceGroup"
$vmName = "myVM"
$newVmSize = "<newVmSize>"
$availabilitySetName = "<availabilitySetName>"

# Check if the desired VM size is available
$availableSizes = Get-AzVMSize `
  -ResourceGroupName $resourceGroup `
  -VMName $vmName |
  Select-Object -ExpandProperty Name
if ($availableSizes -notcontains $newVmSize) {
  # Deallocate all VMs in the availability set
  $as = Get-AzAvailabilitySet `
    -ResourceGroupName $resourceGroup `
    -Name $availabilitySetName
  $virtualMachines = $as.VirtualMachinesReferences | Get-AzResource | Get-AzVM
  $virtualMachines | Stop-AzVM -Force -NoWait

  # Resize and restart the VMs in the availability set
  $virtualMachines | Foreach-Object { $_.HardwareProfile.VmSize = $newVmSize }
  $virtualMachines | Update-AzVM
  $virtualMachines | Start-AzVM
  exit
}

# Resize the VM
$vm = Get-AzVM `
  -ResourceGroupName $resourceGroup `
  -VMName $vmName
$vm.HardwareProfile.VmSize = $newVmSize
Update-AzVM `
  -VM $vm `
  -ResourceGroupName $resourceGroup

Next steps