Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

You cannot remove /dev/loop0 with rm; detach the loop device instead

ErrorCannot delete device /dev/loop0

Short answer

A loop device is a kernel device, not a normal file, so deleting it is the wrong tool. Find what backs it with losetup, unmount any filesystem on it, then detach it with losetup -d. If the unmount says the target is busy, find what is using the mount first.

  • Applies to util-linux 2.39, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List every loop device with the file that backs it, so you know what you are about to free.

    losetup --list
  2. 2Match what you see, then jump to its fix

    1. 1rm or a delete command on /dev/loop0 is refused or has no effectYou are trying to delete it like a fileFix
    2. 2detach is accepted but the device stays in use, or umount was never runA filesystem is still mounted from the loop deviceFix
    3. 3umount: target is busyThe unmount fails because the target is busyFix

What does the error look like?

  • Cannot delete device /dev/loop0
  • umount: /mnt/image: target is busy.

Cause 1 of 3

You are trying to delete it like a file

1Check

A loop device connects a block device to a backing file. The way to free it is to detach it, not delete the node. Look at which loop devices are attached and to what.

losetup --all

2Fix

Detach the device with losetup. This needs root.

sudo losetup --detach /dev/loop0

3Verify

The device no longer appears with a backing file.

losetup --list

Confirmed by man7.org

Did this fix it?

Cause 2 of 3

A filesystem is still mounted from the loop device

1Check

Find out whether something is mounted from it. The losetup manual shows the order: unmount first, then detach.

grep loop0 /proc/mounts

2Fix

Unmount the mount point shown, then detach the device.

sudo umount /dev/loop0
sudo losetup --detach /dev/loop0

3Verify

No backing file is listed for the device.

losetup --list | grep loop0 || echo detached

Confirmed by man7.org

Did this fix it?

Cause 3 of 3

The unmount fails because the target is busy

1Check

A filesystem cannot be unmounted while a process has a file open on it, or uses it as a working directory, or an active swap file lives on it. Stop those first.

sudo umount /mnt/image

2Fix

Close the program, or cd out of the mount point, then unmount. The -d option also frees the loop device in the same step.

cd ~
sudo umount -d /mnt/image

3Verify

The mount point is gone and the loop device is free.

grep /mnt/image /proc/mounts || echo unmounted
losetup --list

Confirmed by man7.org

Did this fix it?

How do I stop it coming back?

  • Detach a loop device with losetup -d as soon as you finish with the image.
  • Unmount before you detach, in that order.
  • Use umount -d when you mounted an image yourself, so the loop device is freed with the unmount.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Servers

dnf cannot install MySQL on Amazon Linux 2023

dnf cannot find a mysql-server package on Amazon Linux 2023

Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.