Devops

How to Clean Up Disk Space: Actionable Tips for AWS EC2

How to Clean Up Disk Space
Actionable Tips for AWS EC2

Managing disk space on EC2 instances involves removing unnecessary data, automating routine cleanups, and using AWS tools to monitor storage. Below we outline manual cleanup strategies, automated periodic cleanups, AWS-native tools for monitoring/storage management, and step-by-step examples for scheduling disk cleanups on both Linux and Windows EC2 instances.

Manual Cleanup Strategies

Overview: Start by identifying what’s consuming space and then remove or archive unnecessary files. Always exercise caution when deleting system files and consider taking an EBS snapshot backup before major cleanups.

Linux Instances (Manual Cleanup)

  • Identify Large Files/Directories: Use commands like sudo du -ah / | sort -rh | head -20 to find the top space consumers. This shows the largest files/directories (in human-readable sizes) so you know where to focus cleanup efforts. For example, you might find large app directories, log folders, or package caches taking up space.
  • Clean Package Caches: Remove cached package files that are no longer needed. For Debian/Ubuntu, run sudo apt-get clean (to clear downloaded .deb files) and sudo apt-get autoremove -y (to remove orphaned packages like old kernels)​. This often frees space by deleting older Linux kernel headers and packages no longer required by any installed software. For RedHat/CentOS/Amazon Linux, use sudo yum clean all to clear YUM caches.
  • Remove Unnecessary Files: Delete any large files you know are not needed (old backups, exports, etc.). Check user home directories, /tmp, and application directories for dumps or temporary files. Be careful to not remove critical system files. If a file is no longer needed by the system or users, use rm to delete it. If the disk is completely full, deleting even a few non-critical files can free space to allow other cleanup commands to run.
  • Rotate or Clear Log Files: Check /var/log for large log files. Archive or delete logs that are no longer needed. It’s better to rotate logs than simply delete active ones – for example, you can use logrotate or manually rotate by renaming the log and restarting the service (or sending proper signals) so it releases the old file​. This prevents issues with services holding open deleted log files. For instance, if access.log is huge, you might rename it and signal the process (e.g., send kill -USR1 to nginx) instead of deleting it while in use​. Ensure your system has log rotation configured (most modern Linux distros do) so logs don’t grow indefinitely.
  • Clear Temporary Files: Clean out /tmp and other temp locations. You can safely remove old files in /tmp or /var/tmp (e.g., sudo find /tmp -type f -mtime +7 -delete to remove temp files older than a week). Also consider clearing application-specific cache directories if applicable. For example, if the instance runs web browsers or other tools, check ~/.cache for large caches.
  • Remove Unused Applications or Data: Uninstall software that is no longer needed, which can free associated disk space. If you have Docker on the instance, remove unused Docker images/containers (docker system prune -a) to reclaim space. Also, delete any leftover installation bundles or old database dumps lying on the server.
  • Check for Deleted Files Still Open: Sometimes disk space is held by processes keeping deleted files open. If your usage (df -h) is high but you can’t find files occupying that space via du, use lsof | grep '(deleted)' to identify any deleted files still in use. For example, a log file that was deleted manually may still be held open by a running process (like java or awslogs). If you find such cases, restart the service or kill the process to truly free the space​.

Windows Instances (Manual Cleanup)

Understanding how to clean up disk space is crucial, especially when managing multiple instances.

  • Disk Cleanup Utility: Leverage the built-in Disk Cleanup (cleanmgr) tool to remove temporary files, system caches, and other unnecessary data. Launch Disk Cleanup from the Start menu or run cleanmgr.exe. In the dialog, select system files (if on a server, click “Clean up system files”) to remove items like Windows Update caches, Temporary Internet Files, download caches, etc. This can free several GB by deleting update installers and temp files. (On Windows Server 2012, you may need to install the Desktop Experience to get the Disk Cleanup GUI.)
  • Clear Windows Update Cache and Old Updates: Over time, the WinSxS (Windows component store) grows with old update files. Use the Deployment Image Servicing and Management tool (DISM) to clean it up. For example, run an elevated command: Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase. The /StartComponentCleanup option immediately removes superseded versions of components (without the default 30-day grace period)​. The optional /ResetBase (on supported systems) permanently deletes all superseded updates, preventing rollback but freeing maximum space. This can reclaim several GB of space used by old Windows updates.
  • Remove Temporary Files: Manually delete files in temporary directories. You can clean %SystemRoot%\Temp (usually C:\Windows\Temp) and each user’s temp folder (e.g., C:\Users\<Username>\AppData\Local\Temp). Also empty the recycle bin for all users. Windows 10/2016+ have Storage Sense (in Settings) which can automatically clear temp files and recycle bin on a schedule – enable this if available.
  • Uninstall Unneeded Programs: Through “Programs and Features,” remove any software that isn’t required. This frees not only the program’s files but often space in ProgramData or AppData associated with it. For example, if the instance was built from a base AMI that included optional components (like language packs or tools not needed for your use case), uninstalling those can save space.
  • Delete or Archive Log Files: Check common locations for log accumulation. For instance, IIS logs reside under C:\inetpub\logs\LogFiles – you can delete or archive old logs if they consume a lot of space. Windows Event logs themselves don’t usually consume huge space, but if needed you can clear them using Event Viewer or PowerShell (wevtutil clear-log <LogName>). Also look at application-specific logs (e.g., SQL Server logs, etc.) and remove or back up those no longer needed.
  • Other System File Cleanup: Disable hibernation if enabled (on servers it’s often off by default) to remove the hiberfil.sys file. You can do this by running powercfg -h off. Also, consider reducing the page file if it’s taking significant space and if memory conditions allow (though typically leave it system-managed unless you’re desperate for space). Finally, ensure no large old user profiles or leftover data remain (you can delete profiles of users who no longer use the server via System Properties > User Profiles or using Delprof2 tool).

Automated Periodic Cleanup Methods

To prevent disk space issues from recurring, implement automated cleanup on a regular schedule. Automated cleanups should be safe and well-tested to avoid deleting important data. Below are approaches for Linux and Windows:

  • Linux – Cron Jobs & Log Rotation: Linux servers can use cron jobs to run cleanup scripts periodically (e.g., nightly or weekly). For example, you might schedule a weekly cron job to run apt-get autoremove and apt-get clean, clear temp files older than a certain age, and rotate logs. Ensure your /etc/logrotate.conf and /etc/logrotate.d/* are configured to rotate and purge old logs – logrotate usually runs via cron daily and can compress or delete old logs so they don’t fill the disk. You can also use tools like tmpreaper or systemd’s tmpfiles.d to automatically clear out /tmp. Always test your script to ensure it only removes intended files.
  • Windows – Task Scheduler & Cleanup Scripts: Windows Task Scheduler can automate disk cleanup tasks. One method is using the Disk Cleanup utility’s command-line mode: run cleanmgr.exe /sageset:1 once to choose cleanup options (e.g. Temporary files, Recycle Bin, Windows Update cleanup), then schedule cleanmgr.exe /sagerun:1 to run at regular intervals (e.g. weekly). This will run Disk Cleanup in the background with your preset options. Alternatively, use a PowerShell script for cleanup (deleting temp files, clearing event logs, running Dism.exe /StartComponentCleanup, etc.) and schedule that script to run via Task Scheduler (ensure the task runs with highest privileges). For instance, a script could remove files in temp folders older than X days and clear the recycle bin, executed every night.
  • Safety Considerations: In automation, include checks to avoid deleting too aggressively. For example, only delete files older than a threshold, and perhaps log the deletions to a file for audit. It’s wise to monitor the results of automated cleanups initially – e.g., have the cron job email the output or the Task Scheduler job log success/failure – so you can catch any issues (like a script that fails to run, or one that mistakenly tries to remove the wrong files). Over time, automated tasks greatly reduce manual effort and keep storage usage in check.

AWS-Native Tools & Best Practices for Storage Management

AWS provides tools to help monitor and manage your instance storage more efficiently:

  • Monitoring Disk Usage with CloudWatch: By default, EC2 instances report metrics like CPU, network, and EBS I/O to CloudWatch, but disk usage and memory are not reported out-of-the-box. To monitor free disk space, use the CloudWatch Agent. AWS recommends installing the CloudWatch agent on your instances to collect disk metrics (e.g., free space in % or MB). Once the agent is configured (it can be set up to report per-volume free space), you can create CloudWatch Alarms to alert you when disk usage exceeds a threshold (e.g., alarm if free space < 10% on any volume)​. This proactive monitoring lets you respond before the disk fills up completely. (Note: AWS earlier provided “monit scripts” for Linux to push custom metrics, but those are deprecated in favor of CloudWatch Agent​.)
  • AWS Systems Manager (SSM) Automation: AWS Systems Manager can automate maintenance tasks on your instances. Using State Manager, you can schedule scripts or commands to run periodically on a fleet of instances. For example, you could use the AWS-RunShellScript document for Linux or AWS-RunPowerShellScript for Windows, with your cleanup commands, and set it as a State Manager Association to run on a schedule (cron or rate expression)​. This means the cleaning tasks are defined in AWS and pushed to the instance on schedule, rather than configured individually on each machine. AWS even provides some Automation runbooks (pre-defined SSM Automation documents) to help with common issues – check AWS documentation or AWSSupport runbooks to see if any exist for disk cleanup or low disk remediation. (For instance, there are AWS Support runbooks that investigate low disk space and suggest actions, which you can run on-demand via the Console or API.)
  • Right-Sizing Volumes: A best practice is to monitor growth trends and resize the EBS volume before it becomes critical. AWS Elastic Block Store allows increasing volume size on the fly for most volume types. If you notice your disk usage trending high even after cleanups, consider expanding the volume. With Elastic Volumes, you can resize an EBS volume without detaching it or restarting the instance in many cases​. After resizing, you just need to extend the filesystem within the OS (for Linux, use growpart and filesystem-specific commands like resize2fs or xfs_growfs; for Windows, use Disk Management or diskpart to extend the volume). Proactively adding storage is often safer and easier than constantly cleaning up under duress. (Remember, you cannot shrink an EBS volume directly – you’d have to create a smaller volume and copy data – so plan capacity increases accordingly.)
  • Offloading and Archiving Data: To manage long-term storage efficiently, offload data that doesn’t need to stay on the instance. For example, archive old application data or logs to Amazon S3, or use Amazon CloudWatch Logs to ship log files off the instance. By moving rarely-accessed files to S3 or a backup volume, you free up the instance’s disk. You can automate this via scripts or AWS Data Lifecycle Manager (for EBS snapshots of volumes with infrequently changed data). The goal is to keep the instance volumes for active data and use AWS’s durable storage for archival.
  • Use Multiple Volumes for Separation: Another best practice is to use separate EBS volumes for different data types (e.g., keeping the OS and application binaries on the root volume, and putting large application data or databases on a separate data volume). This way, a spike in logs or data growth doesn’t fill up the root filesystem and crash the OS. It also allows scaling a specific volume as needed. Ensure you monitor all attached volumes’ usage via CloudWatch agent metrics.
  • AWS Trusted Advisor & Alerts: AWS Trusted Advisor has a check for “Amazon EBS Volumes at Low Utilization” (which is more cost-focused) and for unattached volumes. While it doesn’t warn about full volumes, it’s good practice to clean up unused EBS volumes/snapshots in your account to save cost (not exactly on-instance cleanup, but part of storage management). You can also set up AWS CloudWatch Events/EventBridge rules or AWS Config rules to detect if an instance’s free space drops below a threshold (with the help of the CloudWatch agent metric) and then trigger an SNS notification or an SSM Automation execution to remediate it. This kind of end-to-end automation can, for example, trigger a Lambda or Systems Manager Automation to clear certain logs when an alarm signals low disk space.
  • Plan for Windows Overhead: If you’re running Windows on EC2, be aware that the base OS consumes a lot of space (often ~20–30 GB for Windows Server after updates). The AWS Free Tier default for Windows is a 30GB volume, which can become almost full after system updates​. A best practice is to start with a larger C: drive (or promptly expand it) for Windows instances to avoid constant low-space issues. Cleaning up will help, but as one user noted, a 30GB C: drive “is a useless system” once Windows updates accumulate, and increasing the volume may be the most practical solution​. Keep this in mind when launching new Windows instances.

These proactive measures reflect your commitment to learning how to clean up disk space effectively.

Step-by-Step Automation Examples

Proper execution of these tasks explains how to clean up disk space for Windows instances.

Below are step-by-step examples to set up scheduled cleanup tasks on both Linux and Windows EC2 instances. These examples illustrate how to implement the strategies discussed above in an automated, repeatable way.

Example: Scheduled Cleanup on Linux (using Cron)

Discussing how to clean up disk space can lead to greater awareness of resource management.

Let’s set up a weekly cron job on a Linux EC2 instance to perform routine cleanup.

By following these automated strategies, you will master how to clean up disk space without manual intervention.

Implementing these methods will significantly help you learn how to clean up disk space and enhance your system’s overall efficiency.

Remember that knowing how to clean up disk space is an ongoing process as your environment evolves.

Steps:

As you implement these strategies, you’ll find that knowing how to clean up disk space becomes second nature.

  1. Create a Cleanup Script: On your Linux instance, create a script (e.g., /usr/local/bin/disk_cleanup.sh) with the necessary commands. For example:
    #!/bin/bash
    # Disk Cleanup Script for Linux EC2
    sudo apt-get autoremove -y # Remove old packages/kernels (Debian/Ubuntu)
    sudo apt-get clean # Clear APT cache
    sudo yum clean all # Clear YUM cache (if using Amazon Linux/RedHat)
    sudo find /var/log -type f -name "*.log" -mtime +30 -delete # Delete logs older than 30 days
    sudo journalctl --vacuum-time=7d # Clear systemd journal logs older than 7 days (if applicable)
    sudo find /tmp -type f -mtime +7 -delete # Clear temp files older than 7 days

    Adjust this script to your needs – for example, skip apt/yum commands if not appropriate for your distro, or add application-specific cleanup commands (like clearing old backups or Docker images).
  2. Make the Script Executable: Save the file and run chmod +x /usr/local/bin/disk_cleanup.sh to make it executable. Test it manually (sudo /usr/local/bin/disk_cleanup.sh) to ensure it works and doesn’t remove anything unintended. Check disk free space (df -h) and key directories after running to confirm the effect.
  3. Schedule with Cron: Open the cron table with crontab -e (as root or an appropriate user with permissions). Add an entry for weekly execution, for example:cronCopyEdit0 3 * * 0 /usr/local/bin/disk_cleanup.sh This entry runs the cleanup script every Sunday at 3:00 AM (off-peak hours). Adjust timing as needed. Save the crontab. Cron will now run the script at the scheduled times.
  4. Verify Cron Execution: Ensure your cron service is running. You can check /var/log/syslog or /var/log/cron (depending on distro) to see if the job executes at the next schedule. It’s a good idea to have the script output a short log or use the cron MAILTO feature to email results, so you get notified of any issues (for example, if the script fails due to some package lock or permission issue).

By using cron in this way, your Linux instance will routinely clear out old files and prevent space from being exhausted. You can modify the script over time (e.g., change the age thresholds or add new cleanup tasks) as your instance’s usage evolves.

Example: Scheduled Cleanup on Windows (using Task Scheduler)

Scheduling regular cleanups is one effective way to ensure you know how to clean up disk space efficiently.

Ultimately, it’s your responsibility to ensure you know how to clean up disk space and maintain optimal performance.

We’ll configure Windows Task Scheduler to run the Disk Cleanup utility automatically on a schedule. This example uses Disk Cleanup’s “Sage” modes to automate cleaning.

Steps:

Always remember that knowing how to clean up disk space can prolong the life of your EC2 instances.

  1. Configure Disk Cleanup Settings: Log in to the Windows EC2 instance with an admin account. Open a Command Prompt or Run dialog and execute cleanmgr.exe /sageset:100. This opens the Disk Cleanup settings dialog. Check the boxes for the items you want to clean automatically (e.g., Temporary files, Recycle Bin, Thumbnails, Delivery Optimization Files, Windows Update Cleanup, etc.). Then click OK. This saves those selections under the preset ID 100 in the registry. (You won’t see immediate cleanup happen here – it just stores the preferences.)
  2. Create a Scheduled Task: Open Task Scheduler (Start > Administrative Tools > Task Scheduler). Click “Create Basic Task” (or “Create Task” for more options). Give it a name like “Weekly Disk Cleanup”. For the trigger, set it to Weekly (say, every Sunday at 3:00 AM). For the action, choose “Start a program” and in the Program/Script field enter:
    cleanmgr.exe
    In the arguments, enter:
    /sagerun:100This tells Disk Cleanup to run with the preset #100 we configured. Enable the option “Run with highest privileges” (since system file cleanup needs admin rights). Finish creating the task.
  3. Optional – Use PowerShell Script: (Alternative to using cleanmgr presets) If you prefer more control or additional cleanup steps, you can create a PowerShell script. For example, a script could delete files in %TEMP% and user temp folders, clear the recycle bin, and run the DISM cleanup:
    # Cleanup Script Example (PowerShell)
    Remove-Item -Path "C:\Windows\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue
    Get-ChildItem "C:\Users\*\AppData\Local\Temp\*" -Recurse -Force | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue Clear-RecycleBin -Force -ErrorAction SilentlyContinue
    Dism.exe /online /Cleanup-Image /StartComponentCleanup /Quiet

    Save this as a .ps1 file and create a scheduled task to run powershell.exe -ExecutionPolicy Bypass -File "C:\path\to\YourScript.ps1" on your desired schedule. (Ensure the task runs as an Administrator/System for the DISM part to work, and test the script manually first.)
  4. Verify the Scheduled Task: After scheduling, you can force a run to test it: right-click your task in Task Scheduler and choose “Run”. Disk Cleanup (or your script) will execute in the background. Refresh the Task Scheduler to see the last run result (it should show a Last Run Time and no errors). You can also check if free space increased or if temp files were removed as expected.

Consider these methods as part of your toolkit on how to clean up disk space when necessary.

Using AWS tools will help you learn how to clean up disk space efficiently and effectively manage your resources.

With this setup, Windows will perform an automated cleanup at your chosen interval. This keeps the instance lean by purging transient files regularly. Remember that Disk Cleanup’s Windows Update cleanup might require the task to be run as SYSTEM to remove certain files – using Task Scheduler with highest privileges usually handles this, but if not, consider using Group Policy’s “Scheduled Maintenance” or an SSM document for a more powerful approach.

Applying these AWS-native tools will enhance your skills on how to clean up disk space efficiently.


This guide will walk you through effective practices on how to clean up disk space on your instances.

By combining these manual and automated strategies, you can effectively clean up and manage disk space on AWS EC2 instances. Regular maintenance (automated wherever possible), coupled with AWS monitoring and timely scaling, will ensure your instances have sufficient free space and continue to operate smoothly without disk-full interruptions.

Sources:

  1. AWS re:Post Knowledge Center – “Remediate EC2 instance volume low disk space” (discusses evaluating and freeing space or resizing volumes).
  2. Ask Ubuntu: Recommendation to use sudo apt autoremove to clear old kernels and packages.
  3. AWS Compute Blog: “Automating EC2 Windows EBS Volumes monitoring and alarms” – notes that free disk space is not reported by default and suggests using CloudWatch Agent.
  4. AWS Docs: Elastic Volumes guide – you can increase EBS volume size without downtime on supported instances​.
  5. Kai Wern Chan’s Blog: “AWS EC2 Disk Space Full” – techniques for identifying large files, cleaning apt cache, removing old kernels, and safely handling log files​.
  6. Microsoft Docs: Guidance on reducing WinSxS folder size using DISM’s StartComponentCleanup to remove superseded updates​.
  7. Server Fault: Discussion on Windows EC2 volume size – 30GB C: drive can fill up quickly; Disk Cleanup helps but increasing volume is often necessary​.
  8. Stack Overflow: Best practices for CloudWatch monitoring scripts – AWS now recommends CloudWatch Agent for custom metrics like disk usage​.

These examples will reinforce your understanding of how to clean up disk space effectively.

More posts you would like:

Exploring the Ultimate Smart Home Gadgets: Your Guide to a High-Tech Home

Automated Incident Response: PagerDuty vs Opsgenie

Regularly reviewing how to clean up disk space ensures that your instances remain healthy and responsive.

Follow these steps to understand how to clean up disk space systematically on your Linux EC2 instances.

Make sure to note down how to clean up disk space while setting up your cleanup script.

By learning how to clean up disk space, you can prevent potential issues and maintain system health.

Regularly updating your knowledge on how to clean up disk space will benefit your operational efficiency.

Shubham

Recent Posts

Galaxy Z Fold 7: Irresistible Features You Need to Know!

Samsung Galaxy Z Fold 7 The Future of Foldable Technology The world of foldable smartphones…

4 months ago

Vivo T4x 5G Review – A Bold Mid-Range Marvel Awaits

Vivo T4x 5G Review: Features, User Experience, and Real-World Usage Vivo T4x 5G is the…

4 months ago

7 Key Features of AWS Bedrock: Your Ultimate Guide

AWS Bedrock: The Ultimate Guide to Unlocking AI-Driven Cloud Innovation In today’s fast-paced digital era,…

4 months ago

Karpenter vs Cluster Autoscaler: The Ultimate Showdown

Karpenter vs Cluster Autoscaler Which One is Better for Kubernetes Scaling? Introduction Efficient resource management…

4 months ago

Unlock Your Music: Best YouTube to MP3 Converter Tools

The Ultimate Guide to YouTube to MP3 Converter – Free Tools That Actually Work In…

4 months ago

Thop TV HD: Everything You Need to Know in 2025

Thop TV HD: Everything You Need to Know in 2025 In today’s digital age, entertainment…

4 months ago