Back to Blog

Linux Disk Full: How to Free Up Space on a Server

Linux server disk full? Find what takes up space with df and du, clean up logs, the apt cache and Docker, and deal with deleted open files and inodes.

Nodesty Team5 min readServers
The words Disk Full with df, du and journalctl on a dark Nodesty background

When the disk is full on a Linux server, the symptoms often look unrelated to the disk: the database stops, the site returns a 500 error, applications crash, No space left on device errors appear or even logging in over SSH gets difficult. Fortunately, finding what's taking up space and freeing it takes only a few commands.

This guide covers checking how full the disk is, finding the folders that take up the most space, what you can safely clean up and common situations such as "I deleted the file but no space was freed".

How full is the disk?

After connecting to your server over SSH, run:

df -h

The Use% column shows how full each partition is. You'll usually look at the / row. A value of %100 or close to it is the source of the problem.

Finding what takes up space: du and ncdu

To see the folders that take up the most space, sorted by size:

sudo du -xh --max-depth=1 / | sort -h

-x makes it stay on the same partition. Go into the folder that looks large and repeat the same command (for example /var, then /var/log) to find the culprit.

ncdu is a more convenient option; it lets you move between folders with the arrow keys and see what's taking up space right away:

sudo apt install ncdu
sudo ncdu -x /

To list files above a certain size directly:

sudo find / -xdev -type f -size +500M -exec ls -lh {} \;

The usual culprits

Where?What piles up?
/var/logApplication and system logs
/var/log/journalThe systemd journal
/var/lib/dockerDocker images, containers and logs
/var/cache/aptDownloaded package files
Backup folders, /root, /homeBackups and dumps forgotten on the box
Application foldersCache, session and uploaded files

Cleaning up logs

The systemd journal can grow to several gigabytes over time. To see how much space it takes and shrink it:

journalctl --disk-usage
sudo journalctl --vacuum-size=200M

To set a permanent limit, set SystemMaxUse=200M in /etc/systemd/journald.conf and run sudo systemctl restart systemd-journald.

For application logs, empty a growing file instead of deleting it; if the file is still open, deleting it won't free the space:

sudo truncate -s 0 /var/log/myapp/big.log

You can delete old and compressed logs (files ending in .gz, .1, .2). To keep the problem from coming back, make sure logs are rotated regularly with logrotate; most packages ship with their own logrotate settings, and for your own application's logs you can add a file under /etc/logrotate.d/.

The package cache and old kernels

sudo apt clean
sudo apt autoremove --purge

apt clean deletes downloaded package files; apt autoremove removes packages that are no longer needed and old kernels. On AlmaLinux, the equivalents are sudo dnf clean all and sudo dnf autoremove.

Cleaning up Docker

If you use Docker, most of the space is often under /var/lib/docker:

docker system df
docker system prune

docker system prune deletes stopped containers, unused networks, untagged images and the build cache. If you add -a, every image not used by a running container is deleted too. Think twice before using the --volumes option, which deletes volumes; your databases' data may live there.

To stop container logs from growing without limit, you can cap the log size in /etc/docker/daemon.json:

{
    "log-driver": "json-file",
    "log-opts": { "max-size": "50m", "max-file": "3" }
}

The change applies to newly created containers. We cover installing Docker in our How to install Docker guide.

I deleted the file but no space was freed

If a deleted file is still held open by a running process, the file name disappears but the space isn't freed. To find these files:

sudo lsof +L1

The output shows the process holding the file. Restart the relevant service (sudo systemctl restart service-name) and the space comes back. This is why it's better to empty large log files with truncate than to delete them.

The disk has free space but I can't create files: inodes

If df -h shows free space but you still get the No space left on device error, you may have run out of inodes. Every file uses one inode:

df -i

If IUse% is close to 100%, there are millions of small files, usually PHP session files, cache folders or temporary files that are never cleaned up. To find which folder has the most files:

sudo du --inodes -x / 2>/dev/null | sort -n | tail -15

Backups piling up on the server

Backups taken on the server and never deleted quietly fill up the disk. Move backups somewhere independent of the server and set up a routine that keeps only the last few copies; the details are in our Server backups guide. Include database dumps in that routine too.

Monitor the disk

Rather than stepping in after the disk is full, find out before it fills up. Set up simple monitoring that alerts you when disk usage goes above 80%; we cover the options in our Server monitoring guide. If the server is slow even though the disk isn't full, take a look at our Why is my server slow? guide.

Disk space on Nodesty servers

Nodesty AMD Ryzen 9 9950X VPS and AMD EPYC 7763 VPS plans come with NVMe disks and full root access, so you can run every command in this guide on your own server. Daily backups are free on VPS plans and are kept for 7 days. If your data is growing for good, choose a plan with more disk space.

Frequently Asked Questions

What happens on a server when the disk is full?

Databases can't write new records and may stop, logs aren't written, applications throw unexpected errors and sometimes even logging in over SSH gets difficult. When the disk is full, every service that tries to write new data is affected, which is why it's important to monitor the disk before it reaches 100 percent.

I deleted files but the disk still looks full. Why?

The deleted file is still held open by a running process; the file name is gone, but the space isn't freed until the process closes the file. Find these files with sudo lsof +L1 and restart the relevant service to get the space back.

df shows free space but I can't create files. Why?

You may have run out of inodes rather than disk space. Every file uses one inode, and millions of small files, such as session or cache files, can use up the inodes even when the disk is empty. Check with df -i and clean up the folder that contains a large number of files.

Can I delete the whole /var/log folder?

No. Deleting the folder or the files inside at random can stop services from writing their logs. You can delete old and compressed logs, shrink the systemd journal with journalctl --vacuum-size and rotate growing files with logrotate.

Why does Docker take up so much space?

Unused images, stopped containers, the build cache and container logs that grow without limit pile up over time. You can see what takes up space with docker system df and clean up unused items with docker system prune. Be careful when deleting volumes, as they may contain database data.

What should I do if my disk keeps filling up?

First find out what's growing; it's usually a log that grows without limit or backups piling up on the server. Set up log rotation and make sure old backup copies are deleted. If your data really is growing, moving to a plan with more disk space is the most lasting fix.

Nodesty

About the author

Nodesty Team · Infrastructure & Support

The Nodesty team runs our servers, network and DDoS protection day to day and answers customer support requests around the clock. These guides come from that work.

Related articles

Launch your server now.

Build, scale, and conquer without limits on Nodesty's premium infrastructure.

                                                                                                                                                                                                                            
                                                                                                                                                                                                                            
                                                                                                                                                                                                                            
                                                                                                                             ············································                                                   
                                                ··································································································..........···························                                     
                                       ················..........................................................................................................................··············                             
                                  ·············.................::::::::::::::::::::::::::::::::::::::::::::::....................:::::::::::::::::::::::::::::::::::::::................···········                        
                             ············................:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::............·········                     
                         ············.................:::::::::::::::::::::::::::::::::::::::::::::::::::------------::::::::::::::::::::::::::::::----------------:::::::::::::::::::::..........········                  
                    ············......................::::::::::::::::::::::::::::::::::::------------------------------------------------------------------------------------:::::::::::::.........········                
                ··········.............................................::::::::::::::::::-------------==========================================-----------------------------------:::::::::::........·······               
             ········..................:.....................................::::::::::--------=======+++++++++++++++++++++++++++++++++++++=========================------------------::::::::::.......·······              
           ······.........::::::::::::::::::::................................:::::::-----=====++++xxxxxx**************************xxxxxxxxx++++++++++++++===================------------::::::::.......·······             
          ·····.......:::::::::::::::::::::::::::.............................:::::----===+++xxx****%%%%&&&&&&#############&&&&&&&%%%%%*******xxxxxxxx++++++++++++++==============----------:::::::......······             
         ····......::::::::-------------:::::::::::..........................::::---===+++xx***%%%&&&####@@@@@@@@@@@@@@@@@@@@@@@@####&&&&&%%%%*******xxxxxxxxx+++++++++++++===========--------::::::......·····             
        ····.....:::::::--------------------:::::::::.......................::::---===++xxx**%%&&&###@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@###&&&&%%%%%********xxxxxxxxxxx++++++++++++========------::::::.....······            
        ····.....:::::--------------------------::::::::....................::::---===++xx***%%&&&####@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@###&&&&&%%%%%**********xxxxxxxxxxxxx+++++++++=======------:::::.....·····            
        ····....::::::-------=============---------:::::::::................:::::---===+++xx***%%%&&&#####@@@@@@@@@@@@@@@@@@@@@@@@@#####&&&&&%%%%%%*************xxxxxxxxxxxxxx+++++++++======-----:::::....·····            
         ····....:::::-------================---------:::::::::...............:::::----===++++xxx***%%%%&&&&&&#################&&&&&&%%%%%%%*******************xxxxxxxxxxxxxxxxx++++++++======----:::::....·····            
         ·····....:::::--------=================---------::::::::::..............:::::::-----====+++++xxxxxx**************************xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx++++++++=====-----::::....·····            
          ·····.....:::::--------=================----------:::::::::::.................:::::::::-------=============++++++++++++++++++++++++++++++++++++xxxxxxxxxxxxxxxxxxxxxxx++++++++======-----::::....·····            
            ·····.....:::::::----------===========-------------::::::::::::.........................::::::::::::::::::------------------=============+++++++++++++++++++++++++++++++++=======-----::::.....·····            
              ······......:::::::---------------------------------:::::::::::::........................................:::::::::::::::-----------============+++++++++++++++++++++========------:::::.....·····             
                 ······........:::::::::---------------------------:::::::::::::::::...........................................:::::::::::::------------=============================--------::::::.....······              
                    ·········.........:::::::::::::::-------------::::::::::::::::::::::::.......................................::::::::::::::::--------------------------------------::::::::......·······                
                          ··········............::::::::::::::::::::::::::::::::::::::::::::::::::................................::::::::::::::::::::::::::-------------:::::::::::::::.........········                   
                                  ············..................:::::::::::::::::::::::::::::::::::::::::::.........................:::::::::::::::::::::::::::::::::::::................···········                        
                                             ················.............................................................................................................·················                                 
                                                           ······················................................................................···························                                                
                                                                               ·········································································