GICT knowledge base
Removing Temporary Files in Ubuntu: Effective Techniques for Cleanup
Published
Introduction
Ubuntu and its applications create temporary files during normal operation to store data that’s only needed for a short time. These temporary files help programs run smoothly, but they can accumulate over time and take up valuable disk space. Some programs forget to clean up after themselves, leaving behind files you no longer need. This article shows you how to safely remove temporary files from your Ubuntu system to free up storage space. You will learn where temporary files are stored, which ones are safe to delete, and how to clean them up without affecting your system or personal data.
Prerequisites
Before following this article, ensure you have:
- An Ubuntu machine (any version)
- Administrative privileges (ability to use
sudo) - Basic knowledge of using the terminal
Understanding Temporary Files in Ubuntu
Temporary files serve important purposes but can be safely removed once they’re no longer needed. Understanding what these files are helps you clean them up confidently.
What Are Temporary Files
Temporary files include:
- Browser cache and downloaded data
- Application cache files
- System process temporary data
- Installation temporary files
- Thumbnail images of photos and videos
- Crash reports and debugging information
Where Ubuntu Stores Temporary Files
Ubuntu uses several locations for temporary files:
/tmp– System-wide temporary files that programs create/var/tmp– Temporary files that should survive system reboots~/.cache– Your personal application cache files~/.local/share/Trash– Your deleted files (trash folder)/var/cache– System-wide application cache
Check Your Current Temporary File Usage
Before cleaning up, see how much space temporary files are using on your system.
-
- Open the terminal by pressing
Ctrl + Alt + T. - Check the size of system temporary directories:
- Open the terminal by pressing
$ sudo du -sh /tmp /var/tmp
This shows how much space the main temporary directories are using.
-
- Check your personal cache directory:
$ du -sh ~/.cache
This shows the size of your personal application cache files.
-
- Check your trash folder:
$ du -sh ~/.local/share/Trash
This shows how much space deleted files in your trash are using.
-
- See what’s taking up the most space in your cache:
$ du -sh ~/.cache/* 2>/dev/null | sort -hr | head -10
This lists the 10 largest items in your personal cache directory.
Clean System Temporary Files
Ubuntu stores system-wide temporary files in /tmp and /var/tmp directories. These can usually be cleaned safely.
Clean the /tmp Directory
The /tmp directory contains files that programs create temporarily. Ubuntu automatically cleans some of these files on reboot, but you can clean them manually.
-
- Check what’s in your
/tmpdirectory:
- Check what’s in your
$ ls -la /tmp
-
- Remove files older than 7 days from
/tmp:
- Remove files older than 7 days from
$ sudo find /tmp -type f -atime +7 -delete 2>/dev/null
This removes files that haven’t been accessed in more than 7 days.
-
- Remove empty directories from
/tmp:
- Remove empty directories from
$ sudo find /tmp -type d -empty -delete 2>/dev/null
Clean the /var/tmp Directory
The /var/tmp directory contains temporary files that should persist across reboots, but old ones can be removed safely.
-
- Check the contents of
/var/tmp:
- Check the contents of
$ ls -la /var/tmp
-
- Remove old files from
/var/tmp:
- Remove old files from
$ sudo find /var/tmp -type f -atime +30 -delete 2>/dev/null
This removes files older than 30 days from /var/tmp.
Clean Personal Cache Files
Your personal cache files are stored in your home directory and can often take up significant space.
Clean Browser Cache
Different browsers store cache in different locations.
-
- Clean Firefox cache:
$ rm -rf ~/.cache/mozilla/firefox/*/cache2/*
-
- Clean Chrome cache:
$ rm -rf ~/.cache/google-chrome/Default/Cache/*
-
- Clean Chromium cache:
$ rm -rf ~/.cache/chromium/Default/Cache/*
Clean Application Cache
Many applications store cache files in your .cache directory.
-
- See which applications have the largest cache:
$ du -sh ~/.cache/* 2>/dev/null | sort -hr | head -5
-
- Clean thumbnail cache (image previews):
$ rm -rf ~/.cache/thumbnails/*
$ rm -rf ~/.thumbnails/*
-
- Clean general application cache (be careful with this):
$ find ~/.cache -type f -atime +30 -delete 2>/dev/null
This removes cache files older than 30 days from your personal cache directory.
Empty Your Trash
Your trash folder can contain large files that are still taking up disk space.
-
- Check your trash size:
$ du -sh ~/.local/share/Trash/files/
-
- Empty your trash completely:
$ rm -rf ~/.local/share/Trash/files/*
$ rm -rf ~/.local/share/Trash/info/*
You can also empty trash using the graphical interface by right-clicking the trash icon and selecting “Empty Trash.”
Clean System Cache Files
Ubuntu stores system-wide cache files that can be cleaned to free up space.
Clean Font Cache
Rebuild and clean font cache:
$ fc-cache -f -v
This rebuilds the font cache and removes old cached font information.
Clean Icon Cache
Clear the icon cache:
$ rm -rf ~/.cache/icon-theme.cache
$ gtk-update-icon-cache
Remove Crash Reports and Logs
Ubuntu stores crash reports and old log files that can be removed safely.
Clean Crash Reports
Check crash report size:
$ sudo du -sh /var/crash
Remove old crash reports:
$ sudo rm -rf /var/crash/*
Crash reports are only useful for debugging and can be safely removed.
Clean Old Log Files
Remove old compressed log files:
$ sudo find /var/log -name "*.gz" -type f -mtime +30 -delete
Remove old rotated log files:
$ sudo find /var/log -name "*.1" -type f -mtime +7 -delete
Create a Complete Temporary File Cleanup
You can combine multiple cleanup commands for thorough temporary file removal.
-
- Run a comprehensive temporary file cleanup:
$ echo "Cleaning system temporary files..."
$ sudo find /tmp -type f -atime +7 -delete 2>/dev/null
$ sudo find /var/tmp -type f -atime +30 -delete 2>/dev/null
$ echo "Cleaning personal cache..."
$ find ~/.cache -type f -atime +30 -delete 2>/dev/null
$ echo "Cleaning thumbnails..."
$ rm -rf ~/.cache/thumbnails/* ~/.thumbnails/* 2>/dev/null
$ echo "Emptying trash..."
$ rm -rf ~/.local/share/Trash/files/* ~/.local/share/Trash/info/* 2>/dev/null
$ echo "Cleanup completed!"
Check Results and Monitor Space
After cleaning up, check how much space you’ve freed.
Check disk space before and after:
$ df -h /
Create a simple cleanup report:
$ echo "=== Temporary File Cleanup Report ==="
$ echo "Date: $(date)"
$ echo "Disk usage: $(df -h / | tail -1 | awk '{print $5}')"
$ echo "Temp directories:"
$ sudo du -sh /tmp /var/tmp
$ echo "Personal cache: $(du -sh ~/.cache | cut -f1)"
$ echo "Trash: $(du -sh ~/.local/share/Trash 2>/dev/null | cut -f1 || echo '0B')"
Safety Guidelines for Temporary File Cleanup
Follow these guidelines to clean temporary files safely without affecting your system.
Files That Are Safe to Remove
- Cache files older than 30 days
- Browser cache and temporary internet files
- Thumbnail images and previews
- Files in trash folder
- Crash reports and old log files
- Temporary files in
/tmpolder than a week
Files to Be Careful With
- Never delete files that are currently being used by running programs
- Don’t remove entire cache directories, only old files within them
- Avoid deleting system configuration files
- Don’t remove files from directories you don’t recognize
Best Practices
- Close all applications before cleaning their cache files
- Don’t run cleanup commands on files newer than 7-30 days
- Keep regular backups of important data
- Test cleanup commands on a small scale first
Automate Regular Cleanup
You can set up simple reminders for regular temporary file maintenance.
Manual Cleanup Schedule
- Weekly – Empty trash and clear browser cache
- Monthly – Clean application cache and thumbnails
- Quarterly – Clean system temporary files and crash reports
Quick Cleanup Commands
Keep these handy commands for regular maintenance:
- Quick cache cleanup:
$ find ~/.cache -type f -atime +30 -delete 2>/dev/null - Quick trash emptying:
$ rm -rf ~/.local/share/Trash/files/* ~/.local/share/Trash/info/* - Quick browser cache cleanup:
$ rm -rf ~/.cache/mozilla/firefox/*/cache2/* ~/.cache/google-chrome/Default/Cache/* ~/.cache/chromium/Default/Cache/* 2>/dev/null
Troubleshoot Common Issues
Sometimes you might encounter issues when cleaning temporary files. Here’s how to handle them.
Permission Denied Errors
If you get permission errors, make sure you’re using sudo for system directories:
$ sudo find /tmp -type f -atime +7 -delete
Files in Use Errors
If files are in use, close the applications using them first, then try the cleanup again.
Check which processes are using files in a directory:
$ lsof +D /tmp
Recovery from Accidental Deletion
If you accidentally delete important cache files, don’t worry. Applications will recreate them as needed.
If you need to restore deleted files from trash, look in:
$ ls ~/.local/share/Trash/files/
Monitor Temporary File Growth
Keep track of temporary file usage to know when cleanup is needed.
Create a simple monitoring check:
$ echo "=== Temporary File Status ==="
$ echo "System temp: $(sudo du -sh /tmp | cut -f1)"
$ echo "Personal cache: $(du -sh ~/.cache | cut -f1)"
$ echo "Trash size: $(du -sh ~/.local/share/Trash 2>/dev/null | cut -f1 || echo '0B')"
Signs you need cleanup:
- Personal cache over 1 GB
- Trash folder over 500 MB
- System temp directories over 100 MB
- Browser running slowly or taking long to start
Conclusion
Removing temporary files is a safe and effective way to free up disk space on your Ubuntu system. You learned how to identify temporary files, clean system and personal cache directories, and safely remove files that are no longer needed.
Regular temporary file cleanup prevents storage issues and can improve system performance. The cache files will be recreated by applications as needed, so removing them doesn’t harm your system or lose important data.
Remember to clean temporary files monthly or whenever you notice your system running low on disk space. Focus on browser cache, application cache, and trash files for the most significant space recovery with minimal risk.
Related guides
How to Restrict an SSH Key to One Command
Restrict an SSH key to one command with authorized_keys options. Protect the command and check that unwanted shell and forwarding access are denied.
Linux and CommandsSelf-Hosted Video Processing With FFmpeg: A Practical Guide
Learn how to self-host video processing on a VPS using FFmpeg, with practical commands for conversion, compression, thumbnails, and automated workflows.
Linux and CommandsHow to use nohup: Commands in Linux That Survive Terminal Closes
Keep commands running after you close the terminal. nohup examples with wget, Python, rsync, and Docker on any VPS.
Linux and CommandsHow to Fix “Too Many Open Files” Error on Linux Servers
Learn how to fix the "Too Many Open Files" error on Linux servers by understanding file descriptors, adjusting limits, and diagnosing leaks effectively.
