Next.js Discord

Discord Forum

Image optimization cache too large

Unanswered
Red wood ant posted this in #help-forum
Open in Discord
Red wood antOP
Hello, we're using a docker deployment setup for nextjs and the cached images grow beyond the container storage size.
What's the best way to deal with this and ensure cached images are cleared periodically?

11 Replies

West African Lion
what are you using to cache images
if you’re using ‘next/image’ it will cache into the /tmp/ folder if im not mistaken
just exactly how many images are we talking here
@West African Lion just exactly how many images are we talking here
Red wood antOP
Enough to surpass the 20GB limit
We also had an issue with sharp not functioning properly, so it could've shot up the size requirements
Putting all that aside, we want to be able to control when the cached images are being deleted. Maybe make it be stored for a shorter amount of time, since next/image doesn't support manual deletion, from what I read
West African Lion
Im afraid you’ll have to write a custom script that wipes your cache for you,

the only things that the next config file exposes to you is the image sizes it will cache i believe
Hello! The issue that most people consider to be a leak is actually an accumulation of the file system cache in Linux.

If you examine the Nextjs caching algorithm in detail, you will see that it does not remove files that have already "expired" based on the expiredAt value. The Linux operating system, to optimize file access, tries to keep metadata (inodes) in RAM.

We observe this behavior because most often our application is deployed in an isolated container in a production environment on a cluster with large memory resources, and there are no parallel processes putting pressure on the RAM; as a result, the memory tries to fill up until it starts affecting performance.

I have created a script that automatically cleans up some of the cache files—you can give it a try.
Link: https://github.com/VadimOnix/next-image-cache-cleaner

If this helps you, I would be grateful for your star on my repository ✌️