Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I purge old and outdated cache files? #13291

Open
otakustay opened this issue May 2, 2021 · 21 comments · May be fixed by #14661
Open

Can I purge old and outdated cache files? #13291

otakustay opened this issue May 2, 2021 · 21 comments · May be fixed by #14661

Comments

@otakustay
Copy link

Feature request

On out CI system we upload webpack's cache to a storage after each build and download them on the next build to speed up build.

After about 6 months, we find there is a 800MB cache directory with more than 100 .pack files in this cache, causing slower and slower download time.

What is the expected behavior?

We'd like to see an API to purge outdated cache content from these files, we can then run purge monthly, a slow purge process is acceptable since this runs only once per month.

What is motivation or use case for adding/changing the behavior?

Try to limit cache size in long term.

How should this be implemented in your opinion?

Maybe we should have a {purge: true} in cache configuration

Are you willing to work on this yourself?

Not able to do that

@otakustay
Copy link
Author

There is a maxAge configuration but it fails to work, maybe this is because on each download of cache the ctime and mtime is updated to current timestamp.

This is an example of current large cache:

-rw-rw-r-- 1 ferry ferry 119170275 Apr 29 16:37 0.pack
-rw-rw-r-- 1 ferry ferry   3718917 Jan 25 17:17 100.pack
-rw-rw-r-- 1 ferry ferry   2661947 Jan 22 15:16 101.pack
-rw-rw-r-- 1 ferry ferry   2661947 Jan 22 15:16 102.pack
-rw-rw-r-- 1 ferry ferry   2663375 Jan 22 16:13 103.pack
-rw-rw-r-- 1 ferry ferry   1640456 Jan 25 16:39 104.pack
-rw-rw-r-- 1 ferry ferry  27665503 Jan 25 17:18 105.pack
-rw-rw-r-- 1 ferry ferry   2661890 Jan 25 17:17 106.pack
-rw-rw-r-- 1 ferry ferry  35071612 Jan 25 17:18 107.pack
-rw-rw-r-- 1 ferry ferry   1047161 Jan 25 17:18 108.pack
-rw-rw-r-- 1 ferry ferry   3993968 Apr 29 16:54 10.pack
-rw-rw-r-- 1 ferry ferry   3934125 Apr 29 16:54 11.pack
-rw-rw-r-- 1 ferry ferry  56293783 Apr 29 16:54 12.pack
-rw-rw-r-- 1 ferry ferry      5551 Apr 11 15:30 13.pack
-rw-rw-r-- 1 ferry ferry   3153297 Apr 14 13:14 14.pack
-rw-rw-r-- 1 ferry ferry   7043344 Apr 11 15:30 15.pack
-rw-rw-r-- 1 ferry ferry   3900942 Apr 14 13:14 16.pack
-rw-rw-r-- 1 ferry ferry   3149566 Apr 11 23:56 17.pack
-rw-rw-r-- 1 ferry ferry   7088026 Apr 14 13:14 18.pack
-rw-rw-r-- 1 ferry ferry   3153297 Apr 14 14:03 19.pack
-rw-rw-r-- 1 ferry ferry   1700987 Apr 29 16:54 1.pack
-rw-rw-r-- 1 ferry ferry   2776089 Apr 14 14:04 20.pack
-rw-rw-r-- 1 ferry ferry   2774623 Apr 14 13:14 21.pack
-rw-rw-r-- 1 ferry ferry   3923167 Apr 14 14:03 22.pack
-rw-rw-r-- 1 ferry ferry   3912774 Apr 14 13:14 23.pack
-rw-rw-r-- 1 ferry ferry     46629 Apr 14 19:27 24.pack
-rw-rw-r-- 1 ferry ferry   3903555 Apr 14 14:03 25.pack
-rw-rw-r-- 1 ferry ferry   3923088 Apr 14 19:27 26.pack
-rw-rw-r-- 1 ferry ferry   3913944 Apr 14 14:04 27.pack
-rw-rw-r-- 1 ferry ferry     21186 Apr 14 19:27 28.pack
-rw-rw-r-- 1 ferry ferry   3913865 Apr 14 19:27 29.pack
-rw-rw-r-- 1 ferry ferry  11796650 Apr 29 16:37 2.pack
-rw-rw-r-- 1 ferry ferry   3923090 Apr 15 10:23 30.pack
-rw-rw-r-- 1 ferry ferry   3913867 Apr 14 19:27 31.pack
-rw-rw-r-- 1 ferry ferry  19843125 Apr 15 10:23 32.pack
-rw-rw-r-- 1 ferry ferry    804267 Apr 15 10:23 33.pack
-rw-rw-r-- 1 ferry ferry  77633883 Apr 15 10:23 34.pack
-rw-rw-r-- 1 ferry ferry   8339925 Apr 15 10:23 35.pack
-rw-rw-r-- 1 ferry ferry    915722 Mar 24 16:18 36.pack
-rw-rw-r-- 1 ferry ferry  54512178 Mar 24 16:18 37.pack
-rw-rw-r-- 1 ferry ferry     20272 Mar  1 17:28 38.pack
-rw-rw-r-- 1 ferry ferry   7207882 Feb 25 14:53 39.pack
-rw-rw-r-- 1 ferry ferry  15567825 Apr 29 16:37 3.pack
-rw-rw-r-- 1 ferry ferry   6910049 Feb 25 14:56 40.pack
-rw-rw-r-- 1 ferry ferry   3749063 Feb 25 14:56 41.pack
-rw-rw-r-- 1 ferry ferry   3123047 Feb 26 13:31 42.pack
-rw-rw-r-- 1 ferry ferry   3057770 Feb 26 13:31 43.pack
-rw-rw-r-- 1 ferry ferry   3066902 Mar  1 14:34 44.pack
-rw-rw-r-- 1 ferry ferry   4625389 Feb 26 13:31 45.pack
-rw-rw-r-- 1 ferry ferry   6848282 Feb 27 20:10 46.pack
-rw-rw-r-- 1 ferry ferry   7004505 Feb 27 20:22 47.pack
-rw-rw-r-- 1 ferry ferry   2738477 Mar  1 13:06 48.pack
-rw-rw-r-- 1 ferry ferry   2736264 Feb 27 20:23 49.pack
-rw-rw-r-- 1 ferry ferry   4015881 Apr 29 16:37 4.pack
-rw-rw-r-- 1 ferry ferry   6995269 Feb 27 20:23 50.pack
-rw-rw-r-- 1 ferry ferry   5317760 Mar  1 13:06 51.pack
-rw-rw-r-- 1 ferry ferry      9569 Mar  1 14:34 52.pack
-rw-rw-r-- 1 ferry ferry   5319970 Mar  1 13:20 53.pack
-rw-rw-r-- 1 ferry ferry  13243263 Feb 28 14:35 54.pack
-rw-rw-r-- 1 ferry ferry   3806612 Mar  1 14:08 55.pack
-rw-rw-r-- 1 ferry ferry   2739458 Mar  1 14:08 56.pack
-rw-rw-r-- 1 ferry ferry   3792612 Mar  1 17:26 57.pack
-rw-rw-r-- 1 ferry ferry   4932428 Mar  1 15:25 58.pack
-rw-rw-r-- 1 ferry ferry   4348053 Mar  1 17:26 59.pack
-rw-rw-r-- 1 ferry ferry  18028905 Apr 29 16:37 5.pack
-rw-rw-r-- 1 ferry ferry   7116517 Mar  1 14:34 60.pack
-rw-rw-r-- 1 ferry ferry      9327 Mar  1 15:26 61.pack
-rw-rw-r-- 1 ferry ferry   3810131 Mar  1 17:26 62.pack
-rw-rw-r-- 1 ferry ferry     10025 Mar  1 14:35 63.pack
-rw-rw-r-- 1 ferry ferry   2739458 Mar  1 17:26 64.pack
-rw-rw-r-- 1 ferry ferry  26548396 Mar  1 17:26 65.pack
-rw-rw-r-- 1 ferry ferry   2742727 Mar  1 17:26 66.pack
-rw-rw-r-- 1 ferry ferry   7174612 Mar  1 17:26 67.pack
-rw-rw-r-- 1 ferry ferry    777805 Mar  1 17:26 68.pack
-rw-rw-r-- 1 ferry ferry   3871561 Mar  1 17:28 69.pack
-rw-rw-r-- 1 ferry ferry  10872590 Apr 29 16:54 6.pack
-rw-rw-r-- 1 ferry ferry   2736264 Mar  1 17:29 70.pack
-rw-rw-r-- 1 ferry ferry   6900815 Mar  1 17:28 71.pack
-rw-rw-r-- 1 ferry ferry  51530310 Mar  1 17:29 72.pack
-rw-rw-r-- 1 ferry ferry   1111500 Mar  1 17:29 73.pack
-rw-rw-r-- 1 ferry ferry   4993954 Jan 20 17:41 74.pack
-rw-rw-r-- 1 ferry ferry   3700456 Jan 20 17:43 75.pack
-rw-rw-r-- 1 ferry ferry   3711839 Jan 22 10:29 76.pack
-rw-rw-r-- 1 ferry ferry   3701559 Jan 20 18:46 77.pack
-rw-rw-r-- 1 ferry ferry   3692586 Jan 20 17:54 78.pack
-rw-rw-r-- 1 ferry ferry     33649 Jan 22 16:14 79.pack
-rw-rw-r-- 1 ferry ferry  21270722 Apr 29 16:53 7.pack
-rw-rw-r-- 1 ferry ferry   3702384 Jan 20 18:54 80.pack
-rw-rw-r-- 1 ferry ferry   3693411 Jan 20 18:46 81.pack
-rw-rw-r-- 1 ferry ferry  23651993 Jan 22 10:29 82.pack
-rw-rw-r-- 1 ferry ferry   3701285 Jan 20 18:59 83.pack
-rw-rw-r-- 1 ferry ferry   3692308 Jan 20 18:54 84.pack
-rw-rw-r-- 1 ferry ferry   3713051 Jan 25 16:39 85.pack
-rw-rw-r-- 1 ferry ferry   3701285 Jan 20 19:01 86.pack
-rw-rw-r-- 1 ferry ferry   3692308 Jan 20 18:59 87.pack
-rw-rw-r-- 1 ferry ferry   3703147 Jan 21 17:09 88.pack
-rw-rw-r-- 1 ferry ferry   3694171 Jan 20 19:01 89.pack
-rw-rw-r-- 1 ferry ferry   4026682 Apr 29 16:53 8.pack
-rw-rw-r-- 1 ferry ferry   3704062 Jan 22 16:14 90.pack
-rw-rw-r-- 1 ferry ferry   3703008 Jan 21 17:14 91.pack
-rw-rw-r-- 1 ferry ferry   3694033 Jan 21 17:09 92.pack
-rw-rw-r-- 1 ferry ferry   3702849 Jan 22 10:29 93.pack
-rw-rw-r-- 1 ferry ferry   3703290 Jan 22 10:24 94.pack
-rw-rw-r-- 1 ferry ferry   3694315 Jan 21 17:15 95.pack
-rw-rw-r-- 1 ferry ferry   3712423 Jan 22 15:07 96.pack
-rw-rw-r-- 1 ferry ferry   3703433 Jan 22 10:29 97.pack
-rw-rw-r-- 1 ferry ferry     81079 Jan 25 17:18 98.pack
-rw-rw-r-- 1 ferry ferry      5484 Jan 25 16:39 99.pack
-rw-rw-r-- 1 ferry ferry     87419 Apr 29 16:37 9.pack
-rw-rw-r-- 1 ferry ferry   4606528 Apr 29 16:54 index.pack
-rw-rw-r-- 1 ferry ferry   4606102 Apr 29 16:53 index.pack.old

The same project with a single build locally:

-rw-r--r--  1 otakustay  staff   125M May  2 16:38 0.pack
-rw-r--r--  1 otakustay  staff    77M May  2 16:38 1.pack
-rw-r--r--  1 otakustay  staff    15M May  2 16:38 2.pack
-rw-r--r--  1 otakustay  staff   3.7M May  2 16:38 index.pack
-rw-r--r--  1 otakustay  staff   3.7M May  2 16:38 index.pack.old

@sokra
Copy link
Member

sokra commented May 2, 2021

Yep I know about this problem. MaxAge does cleanup old cache entries, but no longer used files are not removed from disk...

@otakustay
Copy link
Author

Perhaps we can include cache outdated information in output stats

@sokra
Copy link
Member

sokra commented May 2, 2021

Perhaps we can include cache outdated information in output stats

You can set infrastructureLogging: { level: "verbose" } to get a few more lines of logging about the caching.

On short term we want to fix the cache issue by removing all unused files from the cache directory after writing the cache. That sounds easy but actually it a bit tricky since, due to lazy deserialization of the cache, we don't know all files that are currently in use. We might change the cache format a little bit to know these details.

@fforres
Copy link

fforres commented Jul 27, 2021

I'm on the same boat, using filesystem caches to speedup our CI builds.
I'm on a big org and project, so after a week and a bit more, our cache got to ~27gb 😅.

@sokra looking @ the code, I see the file serialization middleware lib/serialization/FileMiddleware is used only fore createFileSerializer inside serialization.js (And also print-cache-file.js, which i'm seeing is a debugging/tool #10400 (comment)).

What would be a good approach for a short term fix?

I have a naive approach to fix this locally passing down a purgeStaleCache prop, reading the files inside the cache dir, comparing against allWrittenFiles and deleting what's not used. Pretty much adding all that in between these lines.

https://github.com/webpack/webpack/blob/main/lib/serialization/FileMiddleware.js#L472-L474

Maybe the idea is for serialization middlewares to be more generic in the long run, so creating/deleting files specifically for cache might be too specific or not ideal? However, in the short time WDYT of this PR? #13875

Either way, any guidance is welcome.

@fforres
Copy link

fforres commented Aug 11, 2021

ping @sokra (cc @otakustay) I have an implementation here #13875

I'd appreciate any revie/comments

@fforres
Copy link

fforres commented Aug 19, 2021

Friendly Ping/reminder! :D

@vankop
Copy link
Member

vankop commented Sep 15, 2021

@sokra is it still an issue?

@sokra
Copy link
Member

sokra commented Sep 16, 2021

Yes

@hiyuki
Copy link

hiyuki commented Dec 21, 2021

In filesystem cache, the unused cache items are now splited to a new cache content file partially, perhaps we need a config to remove it, just like maxGenerations:1 works in memory cache.

@vankop
Copy link
Member

vankop commented Dec 21, 2021

there is an opened PR #14661

@webpack-bot
Copy link
Contributor

This issue had no activity for at least three months.

It's subject to automatic issue closing if there is no activity in the next 15 days.

@fforres
Copy link

fforres commented Mar 22, 2022

image

Can an activity be a comment? 😅

I don't mean to be a nuisance, though @sokra should we be pinging you for @vankop's PR can we help to get it out the door/on a good state? 🙏
Any feedback would be appreciated

@gtwilliams03
Copy link

I have this issue also with the filesystem cache. I am using yarn as our package manager, and I get a .cache folder inside my root .yarn folder which starts to accumulate webpack cache files inside a "client-development" folder. It can grow (in development using the hot reloader) to several gigabytes within even an hour of frequent work. In one case, it crashed my computer because it ate all but a couple hundred megabytes of disk space when I wasn't paying attention and deleting the files in it. I am sure I might be missing some config to make this work correctly - but the performance increase for rebuilds is worth it. 😄

My webpack cache config is just this:

cache: { type: 'filesystem', buildDependencies: { config: [__filename] } }

@gtwilliams03
Copy link

This was from some basic react app editing with HMR on (as you can see the files are massive and they keep getting generated):

03/31/2022  12:50 PM           479,897 0.pack
03/31/2022  12:50 PM            23,825 1.pack
03/31/2022  12:19 PM       419,862,614 10.pack
03/31/2022  12:55 PM       419,564,781 11.pack
03/31/2022  12:20 PM       209,994,897 12.pack
03/31/2022  12:28 PM       349,701,164 13.pack
03/31/2022  12:22 PM       419,281,166 14.pack
03/31/2022  12:47 PM       348,111,675 15.pack
03/31/2022  12:27 PM       420,291,521 16.pack
03/31/2022  12:30 PM       419,893,299 17.pack
03/31/2022  12:22 PM       350,010,939 18.pack
03/31/2022  12:45 PM       486,444,606 19.pack
03/31/2022  12:57 PM            26,324 2.pack
03/31/2022  12:23 PM       419,936,678 20.pack
03/31/2022  12:32 PM       489,689,227 21.pack
03/31/2022  12:25 PM       559,894,249 22.pack
03/31/2022  12:46 PM       419,482,343 23.pack
03/31/2022  12:26 PM       349,740,845 24.pack
03/31/2022  12:33 PM       419,789,055 25.pack
03/31/2022  01:04 PM             6,632 26.pack
03/31/2022  12:44 PM       280,681,288 27.pack
03/31/2022  12:46 PM       419,742,821 28.pack
03/31/2022  12:34 PM       629,483,750 29.pack
03/31/2022  12:15 PM       139,863,894 3.pack
03/31/2022  12:55 PM       139,965,008 30.pack
03/31/2022  12:35 PM       419,833,716 31.pack
03/31/2022  12:47 PM       349,224,640 32.pack
03/31/2022  12:44 PM       139,875,213 33.pack
03/31/2022  01:04 PM       349,655,751 34.pack
03/31/2022  12:47 PM       350,134,766 35.pack
03/31/2022  12:58 PM       349,749,519 36.pack
03/31/2022  12:49 PM       349,998,360 37.pack
03/31/2022  12:57 PM         4,980,803 38.pack
03/31/2022  12:49 PM       419,754,702 39.pack
03/31/2022  12:15 PM       573,955,431 4.pack
03/31/2022  01:02 PM       139,967,958 40.pack
03/31/2022  12:50 PM       489,759,270 41.pack
03/31/2022  01:05 PM       349,756,555 42.pack
03/31/2022  12:52 PM       350,429,307 43.pack
03/31/2022  01:05 PM       349,917,417 44.pack
03/31/2022  01:17 PM       289,926,448 45.pack
03/31/2022  01:07 PM       489,641,922 46.pack
03/31/2022  01:12 PM       279,860,071 47.pack
03/31/2022  01:13 PM        69,993,958 48.pack
03/31/2022  01:15 PM       559,939,441 49.pack
03/31/2022  12:17 PM       279,716,114 5.pack
03/31/2022  01:16 PM       489,997,266 50.pack
03/31/2022  01:17 PM       490,029,050 51.pack
03/31/2022  01:18 PM       700,068,373 52.pack
03/31/2022  01:28 PM       419,923,548 53.pack
03/31/2022  01:27 PM       210,040,370 54.pack
03/31/2022  01:28 PM        70,014,220 55.pack
03/31/2022  01:31 PM       349,996,970 56.pack
03/31/2022  01:32 PM        70,014,244 57.pack
03/31/2022  01:33 PM        70,014,244 58.pack
03/31/2022  01:35 PM       420,069,665 59.pack
03/31/2022  12:18 PM       489,596,494 6.pack
03/31/2022  01:38 PM       420,059,937 60.pack
03/31/2022  01:40 PM        70,010,608 61.pack
03/31/2022  01:42 PM       490,075,130 62.pack
03/31/2022  01:42 PM       493,253,073 63.pack
03/31/2022  12:52 PM            27,541 7.pack
03/31/2022  12:19 PM       419,741,159 8.pack
03/31/2022  12:32 PM       419,883,872 9.pack
03/31/2022  01:42 PM         8,188,963 index.pack
03/31/2022  01:40 PM         8,187,660 index.pack.old

@webpack-bot
Copy link
Contributor

Issue was closed because of inactivity.

If you think this is still a valid issue, please file a new issue with additional information.

@Gavin-Gong
Copy link

Is there any progress on this issue?

@Gavin-Gong
Copy link

As an workaround, I wrote a simple webpack plugin. https://github.com/Gavin-Gong/purge-cache-webpack-plugin
Hope this helps someone who has same issue.

npm i purge-cache-webpack-plugin -D
// webpack config
/** @type {import('webpack').Configuration}*/
const config = {
  cache: {
    type: "filesystem", // only working with filesystem cache
    maxAge: 1000 * 60,
  },
  plugins: [
    new PurgeCacheWebpackPlugin(),
    // ...
  ],
  // ...
};

@evolify
Copy link

evolify commented Feb 22, 2023

Any update ?

@alexander-akait alexander-akait moved this to Priority - Low in webpack 5/6 Apr 26, 2023
@github-project-automation github-project-automation bot moved this from Priority - Low to Ready for Merge in webpack 5/6 Jun 24, 2023
@TheLarkInn TheLarkInn moved this from Ready for Merge to Shipped in webpack 5/6 Jun 28, 2023
@alexandrukis
Copy link

alexandrukis commented Jun 25, 2024

This is marked as shipped, but I don't think it is fixed.

I can only see one PR referencing this issue, which is still opened.

Is there any update on this? I am sorry if I missed anything.

Thank you

@alexander-akait alexander-akait moved this from Shipped to Priority - Medium in webpack 5/6 Jul 10, 2024
@BlackCat1397
Copy link

This issue Is still present on Webpack 5.91.0.
Cache size easily grows to 5 Gb during work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Priority - Medium
Development

Successfully merging a pull request may close this issue.