Cursor AI Dead Code Problem! 30% of My 6000 Lines Are Trash? Need Help for Permanent Cleanup!

I’ve been using Cursor AI to generate files, but I’ve noticed a major issue:
It often creates bloated files — for example, content.js ends up with 4000–6000 lines, and at least 1000 of those lines are dead code.

Even after trying Agent mode 10+ times, it no longer removes unnecessary UI elements. It just hides them using CSS (like display: none) instead of actually deleting them from the codebase.


My goals:

  • Permanently delete unused UI components
  • Reduce file size and complexity
  • Avoid leaving behind hidden but unused code

Question:
Is there any rule, setting, or best practice I can follow in Cursor AI to ensure that unused UI elements and dead code are actually removed, not just hidden?

I’m not very experienced with this, so any help or guidance would mean a lot.

1 Like

your problem doesn’t make a lot of sense (to me), i’ve never seen anything like that. can oyu explain what happened from the beginning?

Like [xpdev] says, I need more info, what is the project, frameworks if any, etc, etc, what the rest of the codebase looks like, do you have a linter?? etc, etc…

Also, to maybe fix the already large ones:

  • Open an ask model that will take the whole file in context (@ it in).

  • Be in “Ask” mode and NOT agent mode. Use this prompt:

Please can you look at this file. There is a lot of code in here that isn't in use. I want to remove it piece by piece. Don't tell me about ALL the code that's not used, let's just focus on one very clear area to start. Come back to me with a concise answer on which part of the code it is and explain why you came to that choice.
  • Then once you’re happy, within the same chat (important), switch to claude sonnet 4 agent and then prompt:
Please implement the above suggested code removal. DO NOT make any improvements in the code elsewhere and be 100% accurate in your work.
  • And then test, if happy, repeat.

Your mileage may vary, but I do this alot with a few different things, doing some analysis with a model I deem better at certain things (Google models are very technical and Claude model agents are great).

1 Like

Limits of AI.

AI isn’t a substitute for rigorous software engineering.

Pretend it’s an intern. WHAT code is redundant? Select it and hit “Add to Chat” and tell it “this code is redundant.” Tell it what you want the code to look like afterwards and give it an example. Be specific.

Split your code out into separate domain specific packages.

Something about your environment is making it think this code is required. That’s why it wrote the code in the first place and why it won’t delete the code now. Figure out what that is.

Try a plan/act approach. Tell it there is redundant code, give it an example, and then have it make a PLAN as a markdown checklist. Ask it to target the tasks to ta junior level developer. Once you’re happy with the plan, make a new chat session and have it execute the plan step by step from the top.

You need to figure out what you want and the LLM can give it to you.