BACKGROUND: I have a minecart rail line that services a few farms which are good distance from spawn (1400 blocks), picking things up and returning them to my storage (sorter). I've done /forceload on all the chunks comprising the minecart line which works great. The line runs continuously no matter where players are. However, I've noticed that the active entity count seems to me to be high (2.3K-2.6K) for just 1-2 players online, and also seeing some "can't keep up" messages on the Spigot 1.18 server. I've done all the server lag prevention I can find online short of using a plugin (e.g. composters on top of hopers, etc.). I think that I need to find a way to reduce the number of active entities in all those forceloaded chunks. Here is a recent Spigot timings paste with just 1 player online: https://www.spigotmc.org/go/timings?url=anitidizit . But still, I could be wrong on the cause of the lag, but the high entity count and related ticks in the timings jumped out at me.
OPTIONS I'M EXPLORING: I could run a command block every so often along the line that kills entities within a certain radius, but with a chunk now delving down to Y=-64, and my rail line being at Y=67, I would need to use a kill radius of something like 131 to get everything, which is quite a ways out on X,Z axis from the rail line, which is not very desirable. I'm also considering trying to ensure there are no dark caves or areas in the chunks to limit mob spawning, but that will be very time intensive given the large area.
QUESTION: I'm wondering if there is a way to either kill only mobs in a certain chunk, from the very top to the very bottom, or some other way to control/reduce the number of mobs along the rail line and in other chunks I've forceloaded (e.g. automated farms). I prefer a command block or configuration option, but I'm not completely opposed to a Spigot plugin.
Thanks.