260

I have file servers which are used to store files. Files might reside there for a week, or for a year. Unfortunately, when I remove files from the server, df command doesn't reflect the freed up space. So eventually, the server gets filled up (df shows 99%), and my script doesn't send any more files there, except there might be a few dozen GB of free space on there.

I got noatime flag on the mounted partitions if that makes any difference.

ᄂ ᄀ
  • 208

17 Answers17

414

Deleting the filename doesn't actually delete the file. Some other process is holding the file open, causing it to not be deleted; restart or kill that process to release the file.

Use

lsof +L1

to find out which process is using a deleted (unlinked) file.

Tombart
  • 2,523
54

as Ignacio mentions, deleting the file won't free the space until you delete the processes that have open handles against that file.

Nevertheless, you can reclaim the space without killing the processes. All you need to do is to remove the file descriptors.

First execute lsof | grep deleted to identify the process holding the file

[hudson@opsynxvm0055 log]$ /usr/sbin/lsof |grep deleted
java       8859   hudson    1w      REG              253,0 3662503356    7578206 /crucible/data/current/var/log/fisheye.out (deleted)

Then execute:

cd /proc/PID/fd

then

[hudson@opsynxvm0055 fd]$ ls -l |grep deleted
total 0
l-wx------ 1 hudson devel 64 Feb  7 11:48 1 -> /crucible/data/current/var/log/fisheye.out (deleted)

The "1" will be the file descriptor. Now type "> FD" to reclaim that space

> 1

You might need to repeat the operation if there are other processes holding the file.

20

If partition has been configured to reserve certain portion of disk space only for root usage, df will not include this space as available.

[root@server]# df -h
Filesystem            Size  Used Avail Use% Mounted on
...
/dev/optvol           625G  607G     0 100% /opt
...

Even after space will be reclaimed by deleting files/directories, non-root user won't be able to write to particular partition.

You can easily check if that's your case by trying to create a file on a device as root and non-root user.

Additionally you can check filesystem configuration by running

tune2fs -l <device> | egrep "Block count|Reserved block count

and calculating actual % on your own.

To change disk % reserved for root-only usage, execute

tune2fs -m <percentage> <device>
luka5z
  • 301
16

The file is still locked by the process opening it. To free up space, do these steps:

  1. Run sudo lsof | grep deleted and see which process is holding the file. Example result:

    $ sudo lsof | grep deleted
    COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF      NODE NAME
    cron     1623 root    5u   REG   0,21        0 395919638 /tmp/tmpfPagTZ4 (deleted)
    
  2. Kill the process using sudo kill -9 {PID}. In above sample, the PID is 1623.

    $ sudo kill -9 1623
    
  3. Run df to check if space is already freed up. If it's still full, maybe you need to wait a few seconds and check again.

Aminah Nuraini
  • 1,089
  • 1
  • 8
  • 15
12

One possibility is that the file(s) you deleted have more references in the filesystem. If you've created hardlinks, several filenames will point to the same data, and the data (the actual contents) won't be marked as free/usable until all references to it has been removed. Before you delete files, either stat them (Entry named Links) or do ls -l on them (should be the second column).

If it does turn out that the files are referenced elsewhere, I guess you'll have to ls -i the file(s) to find the inode-number, and then do a find with -inum <inode-number> to find the other references to that file (you probably also want to use -mount to stay within the same filesystem as well).

2

One reason for missing disk space (a scenario I just encountered myself because I obviously didn't do as I normally do when creating a new array), is this...

Usually I free all available disk space, also the 5% reserved for root (tune2fs -m0), since it's a single user file server system. However, I was suddenly stuck with 0 bytes free according to df, but the difference between total and used space said something else.

Since I was certain I had freed those 5% reserved as a default in at least Fedora (and having some empty folders instead of having the files I had just copied... or thought I had), I started to sweat and search desperately for a way to fix this issue, rebooting, trying "lsof" and that kind of stuff.

Then finally I decided to run tune2fs -m0 on the file system even though I was certain that was not the cause - but it was! A little over 400G became available as it should be. Yeah, I know... my mistake, but nonetheless my comment might be useful to others who forget about this reserved space or strongly believe they've freed it. c",)

Bruno
  • 21
2

Since I know a ton of you are doing this for redhat in /var and gzipping files expecting the FS to shrink, but instead it grows, just make sure you service syslog restart. and

lsof -v file

would show you this anyhow.

1

The other answers are correct: If you delete a file, and space does not get freed, it's usually either because the file is still kept open, or there are other hardlinks to it.

To help in troubleshooting, use a tool that tells you where the drive space is being spent: You can use du to get an overview of where space is going. Even better, use a graphical tool like xdiskusage (there are many like this) to hunt down the culprit. xdiskusage and friends let you drill down into the biggest space hogs to find where space is going.

That way, you'll quickly find files that still occupy space because of a second hardlink. It will also show space occupied by deleted, but open files (as (permission denied), I believe, since it cannot read the file name).

sleske
  • 10,234
1

One more option: The disk might be full due to a process that is continuously creating data: logs, cores and the like. It is possible that space is actually being freed but is immediately filled up. I have actually seen such a case. df in this case simply doesn't give the hole picture. Use du to learn more.

Chen Levy
  • 283
0

Instead of deleting files we can truncate the file something like cat /dev/null> file.log. This will make the size of file as 0 bytes. Even though other processes might be holding this file open but it size is what we will reduce to claim disk space.

0

You can type this, this oneliner, it will capture all process that remains as "deleted", and consequently will kill any of them by doing kill-15 PID.

[user@host]# arr=($(lsof +L1 | awk '{ print $2 }'| grep -v 'PID')); for k in ${arr[@]}; do kill -15 $k; done

Also you can verify its update by doing, [user@host]# watch -n 1 "lsof +L1"

dcubaz
  • 11
0

If you have a btrfs filesystem, you may need to delete the same files in the snapshots. Of course, normally snapshots are here for enabling rollback, but if your filesystem is full with no possibility to move some parts, you may as a workaround identify some parts that you can delete completely to be able to go on.

lalebarde
  • 141
  • 1
  • 8
0

I`m using EXT2, FSCK helped me in this situation. Try shudown -F now , after some restarts and fscks, I see half used space.

-1

To check which deleted files has occupied memory enter the command

 $ sudo lsof | grep deleted

It will show the deleted files that holds memory.

Then kill the process with pid or name

$ sudo kill <pid>
$ df -h

check now you will have the same memory

If not type the command below to see which file is occupying memory

# cd /
# du --threshold=(SIZE)

mention any size it will show which files are occupying above the threshold size and delete the file you will find the memory retained

-3

One line:

kill -9 $(lsof | grep deleted | cut -d " " -f4)
ZaPa
  • 1
-4

If you have Windows 10 as a dual boot, you can try booting into Windows 10, then go to Disk Cleanup, select the proper drive, then click "Clean up system files". This worked for me. Good luck.

Mythos
  • 1
-4

open terminal try this command df -Th next use this command sudo du -h --max-depth=1 / in this command you will find disk usage detail then open as root user delete the file (root-local-share-trash) and delete your file

rilson
  • 1