Quick and easy solution to the Shopware 6 cache growing indefinitely
Today, I just wanted to share a little tip about Shopware 6 with you, regarding the cache.
As you might have noticed, after some time, the cache folder on your server grows and grows. This can be a pain, especially when you are on a tight hosting budget and have a lot of products, e.g. a lot of stuff to cache. Sooner or later, you will run out of disk space and will start to experience some weird stuff, that is hard to debug, like only some pages loading correctly, others just partially and some with errors, that seem totally random. Anyway, these are the symptoms, that I have experienced in connection to disk space issues on cached sites. For me, the lesson learned from this was to always check the disk space, if really crazy things happen. ๐
It is a pity, that Shopware does not have a built-in automatic solution against the cache bloating. As far as I know, in other e-commerce systems like Magento 2, you do not really need to take care of this. Perhaps the authors of Shopware will eventually do something about it, but until that great day comes, we have to.
Of course, in Shopware, you can clear the cache manually like this:
1 |
php bin/console cache:clear |
But even if you execute this command, not everything gets deleted from the var/cache folder. Fortunately, the solution is quite simple – we just have to set up a Cron job to delete the contents of the cache folder regularly, as the Shopware documentation suggests. All we need for this is a command line access. There we have to go to the Cron setup by typing:
1 |
crontab -e |
It does not really matter, from which directory you call this command. When you execute it, you should either see the contents of the crontab file in the pre-selected editor or you will get the choice to select, in which editor do you want to edit the crontab file. For me, as a filthy Windows peasant, the most easy to use is the Nano editor. An empty crontab file, opened in Nano editor, looks like this:
Now use your arrow keys to go down, below the commented text and just write a line, similar to the following:
1 |
50 3 * * * rm -r -f /usr/www/users/someuser/sw6/var/cache/* |
Of course, you need to replace the path from the example with the path to the var/cache subdirectory of your Shopware 6 installation. Then you just have to press Ctrl+O to save the changes. No, it is not a typo, the key combination is indeed Ctrl+O. Apparently, for someone, the standard Ctrl+S was too boring, so they had to put something original in there. But be thankful for it, because in other command line editors, it is even worse. ๐
So after this, just press Enter to save the changes to the filename, that the system offers and that does not really matter and then press Ctrl+X to close the Nano editor and return to the command line. Congratulations, you have set up a Cron job to delete the contents of the Shopware 6 cache directory every night at 3:50! In this case, setting the Cron job timing was pretty easy, but if you are not sure, how to set up a more complex one than our “once per day”, you can always consult the Crontab Guru. Recommended! ๐
I hope this little article on Shopware 6 cache and Cron was useful to you. If you have something to add or want to share your experience with this topic, please use the comments below.
The cache:pool:prune command seems more sensible, because clearing the cache incurs a performance hit for your customers. When clearing the cache, you should also run cache:warmup, but it’s bad because it hammers your server with HTTP requests and if you’ve got many products it may take a while…
Great article in addition to the Shopware 6 documentation.
Clearing the cache by deleting everything is ok but maybe on production environments you also want to warm up the cache afterwards.
For that use case we have the following executable bash script configured as cronjob which I want to share:
#!/bin/sh
echo $HOSTNAME
~/bin/php -v
echo start existing cache info
ls -lrth ~/yourShopwareRootFolder/var/cache
du -hs ~/yourShopwareRootFolder/var/cache/*
echo end existing cache info
echo start rm cache
rm -rf ~/yourShopwareRootFolder/var/cache/*
echo end rm cache
echo start cache:warmup
~/bin/php -f ~/yourShopwareRootFolder/bin/console — cache:warmup –no-ansi
echo end cache:warmup
echo start http:cache:warm:up
~/bin/php -f ~/yourShopwareRootFolder/bin/console — http:cache:warm:up –keep-cache –no-ansi
echo end http:cache:warm:up
The script executes “cache:warmup” and “http:cache:warm:up” (with –keep-cache option) as I am not sure if one or the other would also be sufficient.
Documentation from “bin/console help cache:warmup”:
–ansi|–no-ansi Force (or disable –no-ansi) ANSI output
Documentation from “bin/console help http:cache:warm:up”:
–keep-cache Keeps the same cache id so no cache invalidation is triggered
–ansi|–no-ansi Force (or disable –no-ansi) ANSI output