When writing code, clarity and readability are just as important as functionality. However, in this time and era, developers using Vibe Coding encounter an annoying issue — the editor automatically generates unnecessary comments that add no real value. Over time, these redundant comments can accumulate, making your codebase appear messy, more difficult to maintain, and less professional.

The good news? Cleaning them up is simple. In this article, we’ll walk you through how to remove unnecessary comments in Vibe Coding, why doing so improves code quality, and how it can boost your overall productivity. By the end, you’ll have a much cleaner development environment and a smoother workflow.

Approach

We will create a command that scans all project files, identifies unnecessary comments, and automatically removes them. We are talking about two types of comments here.

  • the single line starting with "// "
  • the single or multiple line starting with "{/* "

Command

declare(strict_types=1);

namespace App\Console\Commands;

use Illuminate\Console\Command;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RegexIterator;
use Symfony\Component\Console\Command\Command as CommandAlias;

final class RemoveComments extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:remove-comments';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Remove all comments from the codebase';

    /**
     * Execute the console command.
     */
    public function handle(): int
    {
        $appPath = base_path('app');
        $this->info("Removing comments from PHP files in {$appPath}");

        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($appPath));
        $filteredPhpFiles = new RegexIterator($files, '/\.php$/');

        ['totalFiles' => $totalFiles, 'modifiedFiles' => $modifiedFiles] = $this->removals($filteredPhpFiles);

        $tsxFilePath = base_path('resources/js');
        $this->info("Removing comments from TSX files in {$tsxFilePath}");
        $tsxFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tsxFilePath));
        $filteredTsxFiles = new RegexIterator($tsxFiles, '/\.tsx$/');

        ['totalFiles' => $tsxTotalFiles, 'modifiedFiles' => $tsxModifiedFiles] = $this->removals($filteredTsxFiles);

        $totalFiles += $tsxTotalFiles;
        $modifiedFiles += $tsxModifiedFiles;

        $this->info("Processed {$totalFiles} files. Modified {$modifiedFiles} files.");

        return CommandAlias::SUCCESS;
    }

    private function removals(RegexIterator $filteredFiles): array
    {
        $totalFiles = 0;
        $modifiedFiles = 0;

        foreach ($filteredFiles as $file) {
            $totalFiles++;
            $filePath = $file->getRealPath();
            $code = file_get_contents($filePath);
            $newCode = $code;

            $extension = pathinfo((string) $filePath, PATHINFO_EXTENSION);

            if ($extension === 'tsx') {
                $newCode = preg_replace('/^\s*\/\/.*$\n?/m', '', $code);
                $newCode = preg_replace('/^[^\S\n]*{\s*\/\*.*?\*\/\s*}[^\S\n]*$\n?/ms', '', (string) $newCode);
                $newCode = preg_replace('/{\s*\/\*.*?\*\/\s*}/s', '', (string) $newCode);
            } else {
                $newCode = preg_replace('/^\s*\/\/.*$\n?/m', '', $code);
            }

            if ($newCode !== null && $newCode !== $code) {
                file_put_contents($filePath, $newCode);
                $modifiedFiles++;
                $this->info("Comments removed from: {$filePath}");
            }
        }

        return [
            'totalFiles' => $totalFiles,
            'modifiedFiles' => $modifiedFiles,
        ];
    }
}

Currently, this command is configured to specifically scan files with the .tsx and .php extensions. However, it is fully extensible and can be easily adapted to target additional file types, allowing you to customize it according to the specific needs of your project

Cleaning up unnecessary comments in your code is a small step that makes a big difference in maintaining a professional and readable codebase. By creating a custom command to automatically scan and remove redundant comments, you can streamline your workflow, reduce clutter, and focus on writing meaningful, efficient code. While I focused on .tsx and .php files in this guide, the approach is flexible and can be adapted to any file type in your project. Implementing this practice not only improves code quality but also enhances collaboration and long-term maintainability.

You can integrate this command directly into your pre-commit hooks or include it as part of your automated formatting workflow. Doing so ensures that unnecessary comments are removed consistently before code is committed or shared, helping maintain a clean and professional codebase across your entire team.