적용 대상: ✔️ Linux VM ✔️ 유연한 확장 집합
Azure에서 VM(가상 머신)을 삭제하는 경우 기본적으로 VM에 연결된 모든 디스크는 삭제되지 않습니다. 이 기능은 의도하지 않은 VM 삭제로 인한 데이터 손실을 방지하는 데 도움이 됩니다. VM이 삭제되면 연결되지 않은 디스크에 대한 요금을 계속 지불하게 됩니다. 이 문서에서는 연결되지 않은 디스크를 찾아서 삭제하고 불필요한 비용을 줄이는 방법을 보여 줍니다.
비고
You can use the az disk show command to get the LastOwnershipUpdateTime for any disk. 이 속성은 디스크의 상태가 마지막으로 업데이트된 시기를 나타냅니다. 연결되지 않은 디스크의 경우 디스크가 연결되지 않은 시간을 표시합니다. 이 속성은 상태가 변경될 때까지 새로 만든 디스크에 대해 비어 있습니다.
관리 디스크: 연결되지 않은 디스크 찾기 및 삭제
다음 스크립트는 ManagedBy 속성의 값을 검사하여 연결되지 않은 관리 디스크를 찾습니다. 관리 디스크가 VM에 연결된 경우 ManagedBy 속성에는 VM의 리소스 ID가 포함됩니다. 관리 디스크가 연결되지 않은 경우 ManagedBy 속성은 null입니다. 스크립트는 Azure 구독의 모든 관리 디스크를 검사합니다. 스크립트가 ManagedBy 속성이 null로 설정된 관리 디스크를 찾은 경우 스크립트는 디스크가 연결되지 않은 것으로 확인합니다.
중요합니다
먼저 deleteUnattachedDisks 변수를 0으로 설정하여 스크립트를 실행합니다. 이 작업을 통해 연결되지 않은 모든 관리 디스크를 찾아 볼 수 있습니다.
연결되지 않은 모든 디스크를 검토한 후 스크립트를 다시 실행하고 deleteUnattachedDisks 변수를 1로 설정합니다. 이 작업을 통해 연결되지 않은 모든 관리 디스크를 삭제할 수 있습니다.
# Set deleteUnattachedDisks=1 if you want to delete unattached Managed Disks
# Set deleteUnattachedDisks=0 if you want to see the Id of the unattached Managed Disks
deleteUnattachedDisks=0
unattachedDiskIds=$(az disk list --query '[?managedBy==`null`].[id]' -o tsv)
for id in ${unattachedDiskIds[@]}
do
if (( $deleteUnattachedDisks == 1 ))
then
echo "Deleting unattached Managed Disk with Id: "$id
az disk delete --ids $id --yes
echo "Deleted unattached Managed Disk with Id: "$id
else
echo $id
fi
done
관리되지 않는 디스크: 연결되지 않은 디스크 찾기 및 삭제
관리되지 않는 디스크는 Azure Storage 계정에페이지 Blob으로 저장되는 VHD 파일입니다. 다음 스크립트는 LeaseStatus 속성의 값을 검사하여 연결되지 않은 관리되지 않는 디스크(페이지 Blob)를 찾습니다. 관리되지 않는 디스크가 VM에 연결되면 LeaseStatus 속성이 Locked로 설정됩니다. 관리되지 않는 디스크가 연결되지 않은 경우 LeaseStatus 속성이 Unlocked로 설정됩니다. 이 스크립트는 Azure 구독의 모든 Azure Storage 계정에서 관리되지 않는 모든 디스크를 검사합니다. 스크립트가 LeaseStatus 속성이 Unlocked로 설정된 관리되지 않는 디스크를 찾은 경우 스크립트는 디스크가 연결되지 않은 것으로 확인합니다.
중요합니다
First, run the script by setting the deleteUnattachedVHDs variable to 0. 이 작업을 통해 연결되지 않은 모든 관리되지 않는 VHD를 찾아 볼 수 있습니다.
After you review all the unattached disks, run the script again and set the deleteUnattachedVHDs variable to 1. 이 작업을 통해 연결되지 않은 관리되지 않는 VHD를 모두 삭제할 수 있습니다.
# Set deleteUnattachedVHDs=1 if you want to delete unattached VHDs
# Set deleteUnattachedVHDs=0 if you want to see the details of the unattached VHDs
deleteUnattachedVHDs=0
storageAccountIds=$(az storage account list --query [].[id] -o tsv)
for id in ${storageAccountIds[@]}
do
connectionString=$(az storage account show-connection-string --ids $id --query connectionString -o tsv)
containers=$(az storage container list --connection-string $connectionString --query [].[name] -o tsv)
for container in ${containers[@]}
do
blobs=$(az storage blob list --show-next-marker -c $container --connection-string $connectionString --query "[?properties.blobType=='PageBlob' && ends_with(name,'.vhd')].[name]" -o tsv)
for blob in ${blobs[@]}
do
leaseStatus=$(az storage blob show -n $blob -c $container --connection-string $connectionString --query "properties.lease.status" -o tsv)
if [ "$leaseStatus" == "unlocked" ]
then
if (( $deleteUnattachedVHDs == 1 ))
then
echo "Deleting VHD: "$blob" in container: "$container" in storage account: "$id
az storage blob delete --delete-snapshots include -n $blob -c $container --connection-string $connectionString
echo "Deleted VHD: "$blob" in container: "$container" in storage account: "$id
else
echo "StorageAccountId: "$id" container: "$container" VHD: "$blob
fi
fi
done
done
done
다음 단계
For more information, see Delete a storage account.