Free up space on your SSD as a developer
Guillaume Briday
3 minutes
If you're a developer with a Mac, it's very likely you've experienced a lack of space on your SSD. Modern tools like Docker, Node, or Brew can be very demanding in terms of space. Let's look at some quick commands to free (a lot of) space safely.
Before we begin, navigate to your folder containing all your projects.
We'll go tool by tool to see how to free up space—let's go!
Node modules
This command calculates the total size of all your node_modules
folders to verify what we're about to delete:
$ find $(pwd) -name "node_modules" -type d -prune -print | xargs du -chs
By definition, these are just dependencies that can be reinstalled at any time. Therefore, we can delete them if we no longer need them, using this command:
$ find $(pwd) -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;
By the way, if you use Time Machine, you can exclude all node_modules
folders with this command:
$ find $(pwd) -type d -name 'node_modules' -prune | xargs -n 1 tmutil addexclusion
Of course, do not delete the node_modules
before running this command.
You can also clear the npm
or yarn
cache:
$ npm cache clean
# or
$ yarn cache clean
Docker
Between containers, volumes, images, and networks, Docker tends to take up a lot of space if you use it regularly.
This command deletes all stopped containers, images, and networks that aren't used:
$ docker system prune -a
Containers
Be careful, you can lose data if you haven't set up a volume correctly.
# List your containers
$ docker ps -a
# Delete your containers
$ docker rm your_container another_container
To list and delete all stopped containers:
# List containers with exited status
$ docker ps -a -f status=exited
# Delete all containers with exited status
$ docker rm $(docker ps -a -f status=exited -q)
To stop and delete all containers:
$ docker stop $(docker ps -a -q)
$ docker rm $(docker ps -a -q)
Images
Like node_modules
, all images can be downloaded whenever you need them, but you can also delete them one by one if needed:
# List your images
$ docker image ls
# Delete your images
$ docker rmi your_image another_image
To delete your untagged images:
$ docker image prune
To delete all images:
$ docker rmi $(docker images -a -q)
Volumes
You might not want to delete your volumes, as they contain your data, and even in development, you don't necessarily want to lose everything. Be careful not to delete anything you need here.
# List your volumes
$ docker volume ls
# Delete your volumes
$ docker volume rm your_volume another_volume
Delete volumes that aren't used by at least one container:
$ docker volume prune
Logs
Logs, even from development, can take up several gigabytes after a while. We rarely need them for very long, and they can be safely deleted.
To see what we're about to delete and check the total size:
$ find $(pwd) -name "*.log" -print | xargs du -chs
To delete them:
$ find $(pwd) -name "*.log" -print -exec rm '{}' \;
Feel free to adapt the command if you have cache folders.
Vagrant
You can quickly end up with several images taking up gigabytes.
# To list your boxes:
$ vagrant box list
# To delete unused boxes:
$ vagrant box remove your_box
Brew
If you use brew, you can delete old formulas:
$ brew cleanup
Ruby
Ruby versions and corresponding gems can take up a lot of space, especially if you frequently work on projects with different versions.
With rvm, you can list installed versions:
$ rvm list
Deleting a version also deletes the corresponding gems:
$ rvm remove 2.4.3 # or another version
To delete only gems:
# To delete obsolete gem versions:
$ bundle clean
# To delete all gems from a gemset:
$ rvm gemset empty
PHP
To delete the composer cache, it's simple:
$ composer clearcache
You can also delete vendor
folders by adapting the node_modules
command. This is risk-free as PHP dependencies can be downloaded at any time.
Conclusion
These commands can help you free up several gigabytes of space, which can be very handy, especially with 128GB SSDs.