Decrease the size of EBS volume in your EC2 instance
Published: 2022-11-21 19:42:09

How to decrease volume size? one of my friend ask me this question. 
 
You might be wondering, can we decrease EBS volume size? The answer is NO. It is impossible to decrease EBS volume size. When you have 100GB EBS and you decide to modify it into 30GB you will get error : **The size of a volume can only be increased, not decreased**. That’s terrible …

But don’t worry. We could also do the trick to decrease EBS volume size. However, it is not easy task as it is not a straightforward. Here is the trick :  

   1. Snapshot the volume
   2. Create a new smaller EBS volume
   3. Attach the new volume
   4. Format the new volume
   5. Mount the new volume
   6. Copy data from old volume to the new volume
   7. Prepare the new volume
   8. Detach and unmount old volume

**Getting started**

Let’s exercise.

Assume we have :

- Instance named my-instance in `ap-southeast-1a` zone
- 100GB EBS volume size named `old-volume`
- We wish to decrease into 30GB and name it `new-volume`
We would need to shutdown the instance to prevent inconsistencies.


**Create a new smaller EBS volume**

   1.  Make sure instance is shutdown
   2.  Go to Elastic Block Store Volumes
   3.  Click Create Volume
   4.  Choose the same volume type same with your old volume
   5.  Enter your desired size; In our case 30.
   6.  Choose availability zone; The volume will be available to instance in the same availability zone, which is ap-southeast-1a .
   7.  Add tag with Key : Name and value : new-volume

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s853111n25zgm01x4wxj.png)
**Attach the new volume**

   1. Right click on the new volume.
   2. Click Attach Volume.
   3. Choose instance name my-instance .
   4. Click Attach.

We could start the instance and login to SSH. List all available volume with `lsblk`. The new volume is in `/dev/xvdf`.

**Format the new volume**

   1. Check whether the volume have any data or not using command `sudo file -s /dev/xvdf` .
   2. If it is displaying `/dev/xvdf: data`, it means the volume is empty. We could format the volume.
   3. If it is displaying other than above output, it means the volume have data. DO NOT format the volume if you saw this output.
   4. Format the volume using command `sudo mkfs -t ext4 /dev/xvdf` .

**Mount the new volume**

   1.  Create directory to mount using command `sudo mkdir /mnt/new-volume` .
   2.  Mount new volume into directory using command `sudo mount /dev/xvdf /mnt/new-volume `.
   3.  Check volume with command `df -h`; The new volume should be mounted now.

**Copy data from old volume to the new volume**  

   1.  Use rysnc to copy from old volume to the new volume `sudo rsync -axv / /mnt/new-volume/`.
   2.  Relax and wait until it’s finished. Get a coffee!

**Prepare new volume** 

   1. Install grub on new-volume using command `sudo grub-install --root-directory=/mnt/new-volume/ --force /dev/xvdf `.
   2. Unmount new-volume `sudo umount /mnt/new-volume `.
   3. Check **UUID** using command `blkid` .
   4. Copy UUID from `/dev/xvda1` (paste anywhere to backup this **UUID**); This is your old **UUID**.
   5. Use tune2fs to replace UUID `sudo tune2fs -U COPIED_UUID /dev/xvdf`; COPIED_UUID is the string value from point 4.
   6. Check the system label from old-volume using command `sudo e2label /dev/xvda1` ; It will display string like cloudimg-rootfs .
   7. Replace new-volume label with old-volume label using command `sudo e2label /dev/xvdf cloudimg-rootfs` .
   8. We can logout SSH now.

**Detach old volume**

     1.  Stop instance `my-instance`
     2.  Detach `old-volume`
     3.  Detach `new-volume`
     4.  Attach new-volume to `/dev/sda1`
     5.  Start instance my-instance
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/40xk60v1g3ypvrkiofas.png)
     After we finish all steps above, we could check our instance by login into SSH.

We could delete `old-volume` and `snapshot-old-volume` if our system works correctly. I prefer to keep snapshot for a week / month before deleting it.

**Conclusion**

We cannot straightforward decreasing the size of EBS volume. All we can do is to create a new smaller volume.

Hope it’s help!