Question

Best way to clear inodes?

I have run out of inodes! I have a folder with millions of smaller files that I would like to keep. However, using df -i I see that I am 100% out of inode space. Can I just clear them? It appears I need to do something since I can’t even download the files through scp or sftp in order to clear them from my remote server.

Ideas? Suggestions?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
May 15, 2024
Accepted Answer

Hey!

Quick update to this question here. I recently answered a similar question about this here:

https://www.digitalocean.com/community/questions/28-no-space-left-on-device-error

Running out of inodes can be a real headache, especially when you have a folder with millions of smaller files. Don’t worry, I’ve got you covered! Here’s a more efficient way to handle this using the find command directly.

  1. Identify Inode Usage: First, let’s identify which directories are consuming the most inodes. You can use the find command combined with wc -l to get a count of the inodes used by each directory.

    find / -xdev -type d -exec sh -c 'echo -n "{}: "; find "{}" | wc -l' \; | sort -n -k2
    

    This command will give you a sorted list of directories with their inode usage.

  2. Drill Down Further: Once you’ve identified the main directory, you need to drill down further. For example, if /home is the culprit:

    find /home -xdev -type d -exec sh -c 'echo -n "{}: "; find "{}" | wc -l' \; | sort -n -k2
    

    Keep drilling down until you find the specific directory causing the issue.

  3. Handle Hidden Files: Include hidden files in your search by specifying the appropriate paths:

    find /home/youruser -xdev -type d -exec sh -c 'echo -n "{}: "; find "{}" -name ".*" | wc -l' \; | sort -n -k2
    
  4. Clean Up: Once you’ve found the directory with the most inodes, it’s time to clean up. Here are a few strategies:

    • Delete Unnecessary Files: Identify and delete files you don’t need. For example, temporary files, logs, or cache files:
      find /home/youruser/temp -type f -delete
      
  5. Automate Cleanups: To prevent this from happening again, you can set up a cron job to regularly clean up unnecessary files:

    crontab -e
    

    Add a job to clean up temporary files every week:

    0 3 * * 0 find /home/youruser/temp -type f -mtime +7 -delete
    
  6. Increase Inode Limit: If the issue persists and you can’t delete any more files, consider resizing your filesystem to increase the inode limit. This is a more complex operation and might require backing up your data.

Here are some commands to help you get started:

# Check inode usage
df -i

# Identify high inode usage directories
find / -xdev -type d -exec sh -c 'echo -n "{}: "; find "{}" | wc -l' \; | sort -n -k2

# Drill down into specific directories
find /home -xdev -type d -exec sh -c 'echo -n "{}: "; find "{}" | wc -l' \; | sort -n -k2

# Include hidden files in search
find /home/youruser -xdev -type d -exec sh -c 'echo -n "{}: "; find "{}" -name ".*" | wc -l' \; | sort -n -k2

# Delete unnecessary temporary files
find /home/youruser/temp -type f -delete

# Compress files
tar -czvf archive.tar.gz /home/youruser/folder

# Move files to another server
scp -r /home/youruser/folder user@other-server:/path/to/destination

# Automate cleanups with cron job
crontab -e

By following these steps, you should be able to clear up some inodes and prevent this issue from recurring in the future.

- Bobby

Your inodes are full, so there’s files somewhere in your server occupying them.

To find what’s consuming inode space, run this:

for i in /*; do echo $i; find $i |wc -l; done

After it returns the sizes of each item, look through them and see which directory is the biggest. Keep drilling down directories a few times until you found the culprit folder. It’s probably log files (error logs/admin logs), PHP session files, or in my case, mod cache disk. I had to keep going into the /var folder, show below.

for i in /var/*; do echo $i; find $i |wc -l; done

Once my search showed that /var/cache/apache2/mod_cache_disk was using almost 100% of my inodes, i deleted the folder and restarted apache and the server, making my inode usage only 4%, down from 100%.

service apache2 restart reboot

alexdo
Site Moderator
Site Moderator badge
May 16, 2024

Heya,

If the problem persist and it’s not related only to the php session files, you can expand the search and check the disk space usage in other directories as well.

You can check which folders are using the most space on the Droplet by using the disk utilization command, du:

  1. du -h --max-depth=1 /

Check our tutorial on How to fix disk space issues here:

https://docs.digitalocean.com/support/how-do-i-fix-disk-space-issues-on-my-droplet/

There is also an ncurses interface for du, appropriately called ncdu, that you can install:

  1. sudo apt install ncdu

This will graphically represent your disk usage:

  1. ncdu
Output--- /root ----------------------------------------------------------------------
    8.0KiB [##########] /.ssh
    4.0KiB [#####     ] /.cache
    4.0KiB [#####     ]  .bashrc
    4.0KiB [#####     ]  .profile
    4.0KiB [#####     ]  .bash_history

You can step through the filesystem by using the up and down arrows and pressing Enter on any directory entry.

Hope that this helps!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.