≡ Menu

Clear Squid Proxy Cache And Re-create the Cache Directories

How do I clear squid cache? How do I re-create the cache directories on a server where Squid version 3.x is currently running in Linux or Unix based server?

You can delete/clear cache and re-create the cache directories on a server using the following procedure. Please note that you need to run additional commands if you are using SELinux.


Step #1: Find Squid cache dir location

Open a terminal and then type:
# grep cache_dir /etc/squid/squid.conf
Sample outputs:

cache_dir ufs /var/spool/squid 100 16 256

Step #2: Restart Squid with an empty cache

Open a terminal and login as root user. Or use the ssh command to login to the remote server. Once logged in type the following command to shutdown squid proxy server:
# squid -k shutdown

Use the following rm command to delete the directory:
# rm -rf /path/to/dir/cache/
# rm -rf /var/spool/squid/

Please note that you need repeat the rm command for each cache_dir location you wish to empty. Next, use the following mkdir command to create directory and set permission using chown command:
# mkdir /var/spool/squid
# chown squid:squid /var/spool/squid

A note about SELinux user

If you are using SELinux, type the following command to restestore file(s) default SELinux security contexts:
# restorecon -vr /var/spool/squid

Creating cache_dir

Finally, to re-create the cache directories, enter:
# squid -z
Sample outputs:

2013/08/24 04:24:19| Making directories in /var/spool/squid/00
2013/08/24 04:24:19| Making directories in /var/spool/squid/01
2013/08/24 04:24:19| Making directories in /var/spool/squid/02
2013/08/24 04:24:20| Making directories in /var/spool/squid/03
2013/08/24 04:24:20| Making directories in /var/spool/squid/04
2013/08/24 04:24:20| Making directories in /var/spool/squid/05
2013/08/24 04:24:20| Making directories in /var/spool/squid/06
2013/08/24 04:24:20| Making directories in /var/spool/squid/07
2013/08/24 04:24:20| Making directories in /var/spool/squid/08
2013/08/24 04:24:20| Making directories in /var/spool/squid/09
2013/08/24 04:24:20| Making directories in /var/spool/squid/0A
2013/08/24 04:24:20| Making directories in /var/spool/squid/0B
2013/08/24 04:24:20| Making directories in /var/spool/squid/0C
2013/08/24 04:24:20| Making directories in /var/spool/squid/0D
2013/08/24 04:24:20| Making directories in /var/spool/squid/0E
2013/08/24 04:24:20| Making directories in /var/spool/squid/0F

Starting squid proxy server

To start squid type:
# squid
OR
# service squid start
Sample outputs:

Starting squid:                                            [  OK  ]

Step #3: Verify Squid is running

Type the following commands:
# netstat -tulpn | grep :3128
# tail -f /var/log/squid/access.log
# tail -f /var/log/squid/cache.log

Share this tutorial on:
{ 9 comments… add one }
  • David Stark November 15, 2013, 6:08 pm

    .. thanks but this is the kinda useless bit of recycled and generic documentation which gets novice admins into trouble.

    Be weary of doing this on a server hardned with selinux.
    Squid will not be able to access the freshly created cache.

    EG (centos 6.x) :
    [root@server /var/spool]# ls -laZ squid.orig/0F/FF/
    drwxr-x—. squid squid unconfined_u:object_r:squid_cache_t:s0 .
    drwxr-x—. squid squid unconfined_u:object_r:squid_cache_t:s0 ..
    [root@server /var/spool]# ls -laZ squid/0F/FF/
    drwxr-x—. squid squid unconfined_u:object_r:var_spool_t:s0 .
    drwxr-x—. squid squid unconfined_u:object_r:var_spool_t:s0 ..

    • nixCraft November 15, 2013, 7:36 pm

      You can simply run restorecon command. The faq has been updated to include a note about SELinux user. Hope this helps and I appreciate your post.

    • Tiao January 8, 2014, 9:48 am

      Novice sysadmins set SELinux to disabled, so, there is no problem at all ;)
      lol

  • PS November 5, 2014, 9:42 am

    This is incorrect information. The user to own the cache directory is not necessarily “squid” as listed above.
    On Ubuntu, by default it is “proxy”, other distros might use “nobody”.
    To find the proper user you need to locate the variable “cache_effective_user” in the config file squid.conf. If this setting is commented out, you’ll have to look at the comments in that file to find the default.

  • kishor October 2, 2015, 12:40 pm

    i follewd all stpes but squid failed to start.

    [root@server ~]# squid -z
    2015/10/02 18:01:30| Creating Swap Directories
    [root@server ~]#

    please help me out

  • steve December 26, 2015, 4:35 am

    squid -z makes the directories
    you then have to run the following:
    service squid start

  • aleksandar nachev January 18, 2016, 4:54 pm

    Hallo,my squid cache is empty.what could cause this?10x

  • OasisInDesert January 31, 2016, 11:55 am

    Thank you very much, this article was very helpful for me.
    Finally I have had recreated cache and started service.
    In my previous tries to recreate cache directory I have had missed restoring security context.
    But one remark to the command for restoring SeLinux security context, we must use ‘R’ recursive flag not an ‘r’ since commands and flags in linux are case sensitive.
    Good Luck!

  • SomeGuy July 8, 2016, 11:04 am

    Another funny I got was when changing to coss cache_dir. Squiddy would for some unexplained reason not start, until I switched back to a folder based option (anyone really, e.g. ufs) and run
    #> squid -z
    to create the cache folders. Now switch to coss and squid starts! Seemed like it was looking for the directories although it was not going to use them under coss.

    I was using squid 2.7 STABLE 5, 7, and 12 at the time on SLES 10 and 11.

    When you create a cache, do not overfill the file system. If you have for instance 20GB partition, the cache should use less than 10GB. To quickly clear the cache you can then rename the cache directory, e.g. /var/cache/squid to /var/cache/squid1, recreate the first, chown as above, ‘squid -z’ for the sub directories and then start squid. So less than 10GB old cache can be deleted later in the background, but you have enough space to start growing a new cache.

    Waiting for a rm command to finish off a 40GB cache often takes too much time (although only minutes) while squid is not running, hence the rename and recreate option. Rebuilding a cache of the same size could also take hours with no service to the users, so rather create a new folder to get going.

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


   Tagged with: , , , , , , ,