User Agents

Web browsers and everyday web tech, explained

Nginx Cache Purge: Wildcards and Disk Cleanup Guide

· 5 min read · Cookies and Cache

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Purging the Nginx cache using wildcards and performing disk cleanup allows you to selectively remove outdated content from your server without restarting the entire service. This process ensures that users see the most recent version of your site while freeing up storage space on the server's disk, preventing performance degradation caused by a full filesystem.

What You'll Need

  • Root or sudo access to the server running Nginx.
  • A terminal client (SSH) connected to the server.
  • Knowledge of the proxy_cache_path defined in your Nginx configuration file (usually found in nginx.conf or /etc/nginx/conf.d/).
  • The find command-line utility (standard on most Linux distributions).

Step-by-Step

  1. Locate the Cache Directory: Open your Nginx configuration file and search for the proxy_cache_path directive. Note the absolute path to the directory where the cache files are stored. For example: proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m .... In this case, the path is /var/cache/nginx.
  2. Identify Target URLs for Wildcard Purging: Nginx stores cache files as MD5 hashes of the cache key (usually the URL). Because the files are renamed to hashes, you cannot simply search for "/blog/*" inside the folder. To purge by wildcard, you must identify the files associated with those paths.
  3. Execute a Wildcard Purge via Find: Since Nginx does not natively support wildcard purging via a simple command, you must use the find command to locate and delete files based on their modification time or by using a script that maps URLs to hashes. To clear all files modified within a specific window (which often correlates to a specific update), use:
    sudo find /var/cache/nginx -type f -mtime -1 -delete
    (This deletes all cache files modified in the last 24 hours).
  4. Manual Disk Cleanup of Old Cache: To perform a full disk cleanup of the cache directory to reclaim space, you can remove the contents of the cache folder entirely. This is the most effective way to ensure no stale data remains:
    sudo rm -rf /var/cache/nginx/*
  5. Implement an Automated Purge Module (Optional): For ongoing wildcard support, install the ngx_cache_purge module. Once installed, you can add a purge directive to your server block:
    location ~ /purge(/.*)
    { allow 127.0.0.1; deny all; proxy_cache_purge my_cache $1; }
    This allows you to trigger a purge via a CURL request to a specific URL.
  6. Verify Disk Space Recovery: Check the available disk space to ensure the cleanup was successful:
    df -h /var/cache/nginx
  7. Reload Nginx Configuration: If you made changes to the configuration file to add a purge module or change paths, reload Nginx to apply them:
    sudo nginx -s reload

Common Mistakes

  • Deleting the Directory Instead of Contents: Running rm -rf /var/cache/nginx instead of rm -rf /var/cache/nginx/* can lead to permission errors or Nginx failing to start because the directory itself is missing.
  • Using the Wrong Cache Path: Attempting to purge /var/lib/nginx when the cache is actually stored in /var/cache/nginx. Always verify the proxy_cache_path first.
  • Purging During Peak Traffic: Massive disk deletions during high traffic spikes can cause temporary I/O wait increases, slightly slowing down response times for users.
  • Forgetting Sudo: Cache directories are typically owned by the www-data or nginx user; attempting to delete files as a standard user will result in "Permission denied" errors.

Tips

  • Use a Cron Job: If your cache grows rapidly, set up a cron job to clear the cache every week or month to prevent the disk from filling up.
  • Set a Max Size: Use the max_size parameter in your proxy_cache_path directive (e.g., max_size=10g). Nginx will automatically delete the least recently used (LRU) items when the limit is reached.
  • Test with -print: Before running a find ... -delete command, replace -delete with -print to see exactly which files will be removed.
  • Layered Caching: If you use a CDN (like Cloudflare) in front of Nginx, remember that purging the Nginx disk cache does not purge the CDN cache. You must do both.

Troubleshooting

  • Files Not Deleting: If files remain after a rm -rf command, check for immutable flags using lsattr. If files are immutable, use chattr -i [filename] before deleting.
  • Nginx Error Logs: If Nginx behaves strangely after a cleanup, check the error logs: sudo tail -f /var/log/nginx/error.log. Look for "Permission denied" or "No such file or directory" errors.
  • Cache Not Refreshing: If you purged the disk but still see old content, check your browser cache or a downstream CDN. Use curl -I [URL] to check the X-Cache header to see if the response is coming from Nginx.
  • High CPU during Purge: If find is consuming too many resources on a massive cache, use ionice to lower the I/O priority: sudo ionice -c 3 find /var/cache/nginx -type f -delete.

Frequently Asked Questions

Does purging the cache restart Nginx?

No. Deleting the files in the cache directory is a filesystem operation and does not require the Nginx service to be restarted or reloaded.

Can I purge a specific folder within the cache?

Because Nginx uses a hashed directory structure (levels), you cannot easily map a URL folder to a physical folder. You must either purge the entire cache or use a specific purge module.

How often should I perform disk cleanup on the cache?

If you use the max_size directive, Nginx manages this automatically. If not, you should clean it whenever disk usage exceeds 80% of your partition capacity.

Will purging the cache slow down my site?

Temporarily, yes. Since the cache is empty, Nginx must fetch content from the backend server for every request until the cache is repopulated, which increases latency.

Is there a way to purge cache via a GUI?

Standard Nginx does not have a GUI. However, control panels like Plesk or cPanel provide buttons to clear the Nginx cache for specific domains.