Errors without a clear cause are the most irritating problems to deal with. And while Linux is usually specific in its error messages, this is one of the few times when it drops the ball.

Getting a “No place left on device error” explains very little, especially if your drive is far from full. Why is this error message showing up? Why can’t you create new files despite having sufficient space on the disk? And most importantly, how do you fix it?

Table of Contents

    Let’s find out.

    Does Your Hard Drive Actually Have Enough Space?

    Before you rush to find a fix, make sure there is a problem, to begin with. After all, if disk space has actually run out on your system, there is no need to panic. You can just remove unnecessary data to free up more space.

    There are two Linux system commands for getting information about disk space – du and df. The du command estimates the disk space usage, while the df command analyzes the free space present on the disk. Using them in conjunction with sudo gives you an accurate report of how much space is actually available on the disk.

    1. Let’s start with the du command. Its output can be rather long and cumbersome to go through, so we will use the -s and -h flags. -s summarizes the results, while -h ensures it is human readable. So enter the command sudo du -sh / where / points to the base directory.
    1. Don’t worry about all the permission denied messages – that’s how it trawls through all subdirectories of the system. Depending on the size of the drive you may have to leave it running for a while, as it recursively goes through everything.
    1. When du finishes scanning, it will output a simple list of directories alongside their sizes. For example, this is the output of the du command applied to an ordinary directory.

    Now we will use df to see how much free space is available. The df command is much simpler to use since it only shows the mounted filesystems and their usage statistics. Once again, we will use the -h flag to make the output human readable.

    sudo df -h

    The idea is to tally the results of the du and df commands. Any discrepancy points to disk space that is not freely available despite not being used by any files or folders.

    Why Does Linux Show “No Space Left on Device”?

    If you are seeing the “No Space Left on Device” error message despite having sufficient free space on your disk, it’s not the hardware that’s at fault. Figuring out the exact cause for the error can take a bit of troubleshooting.

    • Recently Deleted File: The most common reason for seeing this error is a recently deleted file. Often a file is deleted while a process is still using it, keeping the space reserved even though the file is already gone.
    • Not Enough Inodes: Another frequent cause is not having enough inodes. Inodes are the index pages of a Unix filesystem, holding the metadata of every file on storage. However, inodes aren’t unlimited, and running out of inodes before the storage space can give you the “No space left on device” error.
    • Failing Hard Drive: Then of course there is the possibility that the hard drive is simply failing, and much of the apparent free space is taken up by bad sectors. Since the system fails to write any files to these locations, it throws an error.

    Fix 1: Restart Processes Using Deleted Files

    The most probable cause of the “No space left on device” error is a process still using a deleted file. Thankfully, fixing this error is easy. You just need to restart the process to free up the reserved storage.

    1. To find the problematic process, you need to use the lsof and grep commands. The lsof command will give you a list of all open files being used by running processes, and grep can narrow down the selection to the ones that are deleted. So enter sudo lsof / | grep deleted where / is the base directory and | a pipe for linking lsof’s output to grep.
    1. Now you can restart the affected process with the sudo systemctl restart service_name command, where the service name is the name of the service that turned up in the search.
    1. If there are multiple processes like this or you just don’t want to bother with finding specific services, you can just reset them all with the sudo systemctl daemon-reload command. This regenerates all dependencies, taking any changes in the filesystem into account.

    After this the storage space being held up by the process should be available again, allowing you to write files without running into any errors.

    Fix 2: Check Inodes

    While each drive has a large number of inodes, it is finite. And if your system is littered with an incredible number of files, it is possible to reach this limit before exhausting the storage capacity of the drive. This is why it’s better to have large files rather than too many small ones.

    You can easily check the availability of inodes by using the -i flag with the df command. Like this:

    sudo df -i

    This will clearly tell the total number of inodes present in the filesystem, along with the amount currently in use. If there are no free inodes left on your system, this is the source of the “No space left on device” error.

    As inodes are only created upon first formatting the drive, there is no way to generate more inodes. All you can do is delete any unnecessary files to free up inodes for future files.

    Fix 3: Mark Bad Blocks

    Data corruption is the inevitable fate of any hard drive. While a new drive will not run into this issue, older hard disks will gradually start going “bad”.

    What this means is that portions of the hard drive are rendered unusable, even though the filesystem still considers them functional. These bad blocks falsely inflate the apparent free space on the drive, when in truth no files can be written to them. This leads to a “No space left on device” error, as the operating system tries and fails to store any data on these locations.

    There is no real way to fix this issue, as bad sectors are a result of the physical wear and tear of a hard drive. The only thing you can do is mark the bad sectors so that they are no longer referenced by the file system.

    Doing this requires you to boot from a Live CD, as you cannot perform a File System Check within a running drive. Once you do that, just use the following command:

    sudo fsck -vcck /dev/sda

    This replaces the /dev/sda with the path of the drive you want to repair. This will automatically detect all bad blocks on the drive and mark them as unusable. It won’t give you any extra storage capacity, however, so be prepared to clean up some useless files to free up space on the drive.

    What Is the Best Way to Fix the “No Space Left on Device” Error?

    The usual reason for seeing a “No space left on device” error on Ubuntu or any other Linux distro is a deleted file still being used by a running process. This reserves the storage that was occupied by the file, preventing other operations from writing data to that space.

    Fixing this problem is also the easiest, as all you need to do is to restart the process in question. The other causes of the error, however, are not that easy to solve.

    Whether your drive is plagued by bad data blocks or has run out of inodes, there is no direct way to fix these issues. You must delete existing files to make room for new data to be written.

    Leave a Reply

    Your email address will not be published. Required fields are marked *