Shopware 6 - Tips, tricks and how to's

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:

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:

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:

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.