You can detach a data disk from an Elastic Compute Service (ECS) instance when the instance no longer needs the disk. You can also detach a data disk from an ECS instance and then attach the disk to a different ECS instance within the same zone. This topic describes how to detach a data disk.
Prerequisites
Before you detach a data disk, make sure that the following conditions are met:
The data disk is attached to an ECS instance and In Use is displayed in the Status column that corresponds to the disk in the ECS console.
To prevent data loss and ensure data integrity, we recommend that you stop read and write operations on the data disk before you detach the disk.
The data disk is a cloud disk or an elastic ephemeral disk. You cannot detach local disks that are used as data disks.
Workflow
The following figure shows the workflow of detaching a data disk.
If the data disk that you want to detach uses the subscription billing method, change the billing method of the disk to pay-as-you-go.
For more information, see Change the billing method of a cloud disk.
Unmount all file systems on the data disk in the operating system.
For more information, see the Step 2: Detach the data disk from within the operating system of the associated ECS instance section of this topic.
After you detach the data disk from within the operating system of the associated ECS instance, detach the disk from the instance in the ECS console.
For more information, see the Step 3: Detach the data disk in the ECS console section of this topic.
Procedure
(Conditionally required) Step 1: Change the billing method of the data disk from subscription to pay-as-you-go
If your data disk uses the pay-as-you-go billing method, skip this step.
If your data disk uses the subscription billing method, you cannot detach the data disk, as shown in the following figure. In this case, you must change the billing method of the data disk to pay-as-you-go. For more information, see Change the billing method of a cloud disk.
Step 2: Detach the data disk from within the operating system of the associated ECS instance
If the data disk is partitioned and file systems on the disk are mounted, perform the following operations to detach the data disk from within the operating system of the ECS instance to which the disk is attached.
Linux
Connect to the ECS instance.
For more information, see Use Workbench to connect to a Linux instance over SSH.
Run the following command to view the mounting information of the data disk:
df -h
The following sample command output is returned. In this example, the /dev/vdb1 partition of the data disk is used. In actual scenarios, select the data disk partitions whose mounting information you want to view.
Run the umount command to unmount the file systems of the data disk partitions.
For example, you can run the following command to unmount the file system of the /dev/vdb1 partition:
umount /dev/vdb1
Run the following command to view the UUIDs of the data disk partitions:
blkid
The following sample command output indicates the UUID of the /dev/vdb1 partition of the data disk.
Run the following command to check whether the /etc/fstab file contains the automatic mounting configurations of the file systems in the data disk partitions:
cat /etc/fstab
Find the UUID that you obtained in the previous step in the command output. The file system of the /dev/vdb1 partition is configured in the /etc/fstab file, as shown in the following figure.
Delete the automatic mounting configurations of the data disk that you want to detach from the /etc/fstab file.
NoteIf you do not delete the automatic mounting configurations of the data disk from the /etc/fstab file, the ECS instance cannot be restarted after you detach the data disk from the instance.
Run the following command to modify the /etc/fstab file:
vim /etc/fstab
Press the
I
key to enter Insert mode.Delete or comment out the automatic mounting configurations of the data disk that you want to detach.
For example, you can add a number sign (
#
) before a line that contains an automatic mounting configuration to comment out the configuration, as shown in the following figure.Press the Esc key, enter
:wq
, and then press the Enter key to save and close the file.
Windows
Connect to the ECS instance.
For more information, see Use Workbench to connect to a Windows instance over RDP.
On the Windows desktop, right-click the
icon and select Disk Management.
In the Disk Management window, right-click the disk that you want to detach and select Offline.
Step 3: Detach the data disk in the ECS console
To detach a data disk, you can go to the Instance or Block Storage page. This section describes how to detach a data disk on the Instance page.
Go to ECS console - Instance.
In the top navigation bar, select the region and resource group of the resource that you want to manage.
Find the stopped ECS instance from which you want to detach a data disk and click the instance ID.
Click the Block Storage tab.
Find the data disk that you want to detach and choose Detach in the Actions column.
NoteIf No is displayed in the Removable column that corresponds to the data disk, check whether the disk uses the subscription billing method. If the data disk uses the subscription billing method, you must change the billing method of the data disk to pay-as-you-go before you can detach the disk. For more information, see Change the billing method of a cloud disk.
In the message that appears, click OK.
ImportantIf the disk is already offline from the Windows instance but an error is reported when you detach the disk in the ECS console, programs on the instance may be accessing the data on the disk. We recommend that you restart the instance, take the disk offline, and then detach the disk in the ECS console. Instance restart may interrupt services. We recommend that you restart the instance during an appropriate period of time.
If Unattached is displayed in the Status column that corresponds to the data disk after you perform the preceding steps, the disk is detached.
References
You can call an API operation to detach a pay-as-you-go data disk from an ECS instance. For more information, see DetachDisk.
You can attach the data disk that was detached from an ECS instance to a different ECS instance within the same zone. For more information, see Attach a data disk.
If you no longer require the data disk that you detached, back up the data on the disk and then release the disk. For more information, see Create a snapshot and Release a disk.