Clear Squid Proxy Cache And Re-create the Cache Directories

by on August 25, 2013 · 4 comments· LAST UPDATED November 15, 2013

in , ,

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.

Tutorial details
DifficultyEasy (rss)
Root privilegesYes
RequirementsNone
Estimated completion time5m

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

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 4 comments… read them below or add one }

1 David Stark November 15, 2013 at 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 ..

Reply

2 nixCraft November 15, 2013 at 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.

Reply

3 Tiao January 8, 2014 at 9:48 am

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

Reply

4 PS November 5, 2014 at 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.

Reply

Leave a Comment

Tagged as: , , , , , , ,

Previous Faq:

Next Faq: