Safely removing SATA hard drive from a running system

I have a Debian-based system with a SATA controller running in AHCI mode. I sometimes need to plug and unplug a disk from an internal SATA bay. The bay is nothing fancy – you just slide a disk cradle into it and the SATA and power sockets make contact.

Wyzard on Unix & Linux Stack Exchange
has a good advice on how to do a proper hot unplug. The following is based on
that advice:

First you need to unmount the partitions. Then, deactivate any LVM groups with vgchange -an – I don’t use LVM, so I haven’t tried that. Finally, it is a very good idea to unregister device from the kernel and spin it down by

echo 1 > /sys/block/(device name)/device/delete

Then I can hear the disk spin down, wait for it to stop completely and just pull it out. When I put in back in, it registers properly. Please note, that I am using AHCI mode in the controller.

How to spin down USB hard drive for safe removal

Let’s assume you are running a Debian-based system and have a hard drive at /dev/sdc with a partition /dev/sdc1
mounted. To safely remove the hard drive, first you need to unmount all its
partitions. In this case you may do:

udisks --umount /dev/sdc1

and then spin the disk down with:

udisks --detach /dev/sdc

You should the notice (usually, hear) the disk spin down.

Please note, that we are referring here to the device sdc not its partition
sdc1.

Thanks, user76204.