Laravel: Automatic Vendor Cleanup Command

Sarfraz Ahmed    January 25, 2018 12:16 AM

When installing composer packages, they come up with lot of useless files and folders such as .git, tests, readme.md and more. When project becomes bigger with many packages, this junk takes considerable disk space. Since I work with Laravel framework most of the time, I created a command that allows me to automatically delete all these junk files whenever I happen to install or update composer packages.

Here is the pattern used by glob function to detect and delete all junk files:

protected $patterns = [
    '.git',
    '.github',
    'test',
    'tests',
    'travis',
    'demo',
    'demos',
    'license',
    'changelog*',
    'contributing*',
    'upgrading*',
    'upgrade*',
    '.idea',
    '.vagrant',
    'readme*',
    '_ide_helper.php',
    '{,.}*.yml',
    '*.yaml',
    '*.md',
    '*.xml',
    '*.log',
    '*.txt',
    '*.dist',
    '*.pdf',
    '*.xls',
    '*.doc',
    '*.docx',
    '*.png',
    '*.gif',
    '*.jpg',
    '*.bmp',
    '*.jpeg',
    '*.ico',
    '.php_cs*',
    '.scrutinizer',
    '.gitignore',
    '.gitattributes',
    '.editorconfig',
    'dockerfile',
    'composer.json',
    'composer.lock',
];

The command itself can be seen here.

To automatically cleanup junk files/folders when installing or updating composer packages in Laravel, @php artisan vendor:cleanup should be added under the post-autoload-dump section in composer.json file. Or we can also trigger it manually anytime by typing php artisan vendor:cleanup --o where --o is optional argument to display verbose output.

If you actually want to be able to use this idea of cleanup of useless files in any PHP project (Laravel or not), you might want to create a composer plugin instead and install it globally that can then work across PHP projects and cleanup useless files from the vendor folder.

If you witness any issue or something missing in the pattern list shown above, please leave your suggestion/improvements in comments. Thanks