Don't run laggy things inside the task. That's the best we can answer from your question. Provide more information so that people can answer your question more specifically.
Is that laggy? If it is, what about spreading the entity clearing to a few more ticks? In addition, are you referring to server lag or client lag? If it is the client lag, you should run the entity clearer more frequently.
or just dont use it!!!!!!!!!!!!!!!!!!!!!!!!!!/s maybe more frequent but less clearings, like only clear 40% of the items by item age or to limit the time of your forloop for each runs