How do I clear fastcgi_cache with PHP?

13,342

If you have

fastcgi_cache_path  /tmp/nginx keys_zone=myzone:8m

just call

rm -Rf /tmp/nginx/*

It's really as simple as this: When you want to clean the cache, clean the cache :) (That in this case is just a folder)

Share:
13,342
amandawulf
Author by

amandawulf

Updated on June 09, 2022

Comments

  • amandawulf
    amandawulf almost 2 years

    Is there a built-in way to clear the Nginx fastcgi_cache with PHP? I know I can write a PHP script that goes through and manually deletes all the cache files, but that seems too much like a hack.

  • amandawulf
    amandawulf over 11 years
    Thanks! I'm trying to do this with PHP on the fly, though. Is there a built-in PHP function, or would you just have to use the PHP unlink function to delete the files?
  • KingCrunch
    KingCrunch over 11 years
    ehm... technically unlink() is the built-in function. When you are talking about the recursion, have a look at this comment: php.net/manual/de/function.unlink.php#101261 The factcgi-cache has a fixed depth, thus you can just do something like array_map('unlink', glob("/tmp/nginx/*/*/*"));. The number of * depends on your configuration. You can keep the empty directories, they usually doesn't hurt :)
  • SkarXa
    SkarXa about 9 years
    Warning: Deleting the cache manually will cause a lot of entries in error.log, because when nginx wants to purge them they dont exist. Example [crit] 8282#0: unlink() "/etc/nginx/cache/route" failed (2: No such file or directory)
  • Matt Fletcher
    Matt Fletcher over 4 years
    I found I had to also restart nginx as it went completely nuts when I deleted the folder
  • Zane Claes
    Zane Claes over 3 years
    If you're doing this with PHP, your site has a security vulnerability. The nginx and PHP user spaces should be separate. This setup would allow an attacker to potentially manipulate the cache.