How to delete build Folders in your Flutter projects using a script
The build folders in Flutter projects sometimes become so big, it eats up your storage. We will look at how to resolve this with a script.
There was this time my Macbook was completely out of storage and when I used cleanMyMac to investigate, I realised the culprit was the build folders in Flutter projects. After deleting a number of them, I had some extra space.
Then I thought to myself, what if I get a script for this because I will still be working on most of these projects so the issue may come up again? Moving forward, I made a script to do just that. This has been optimized but it should work just fine.
You can delete the build folder on a Macbook/Linux-based OS with this script:
#!/usr/bin/env bash
# This script will delete all "build" folders in directories that have a "pubspec.yaml" file
set -e # Exit immediately if a command exits with a non-zero status
# Find all directories that have a pubspec.yaml file and store the paths in an array
dirs=($(find . -name pubspec.yaml -exec dirname {} \;))
# Loop through the array of directories and delete the "build" folder if it exists
for dir in "${dirs[@]}"
do
build_dir="$dir/build"
if [ -d "$build_dir" ]; then
rm -r "$build_dir"
echo "Deleted $build_dir"
fi
done
To use this script, copy and paste the code into a file called delete_build_folders.sh
and save it in the root directory of your Flutter project. Then, open a terminal window, navigate to the root directory of your Flutter project, and run the script using the following command:
bash delete_build_folders.sh
This script will search for all directories that have a pubspec.yaml
file and delete the build
folder in each of those directories.
Note: This script uses the rm
command to delete the build
folders. This is a permanent operation and the files and folders in the build
folder will not be recoverable. Be sure to use this script carefully, and only if you are certain that you want to delete the build
folders in your Flutter projects.
To use the same script on a Windows laptop, you will need to make a few changes to the script. Here is the modified version of the script that you can use on a Windows laptop:
@echo off
rem This script will delete all "build" folders in directories that have a "pubspec.yaml" file
rem Find all directories that have a pubspec.yaml file and store the paths in a temporary file
for /R %i in (.) do (
if exist "%i\pubspec.yaml" (
echo %i >> temp_dirs.txt
)
)
rem Read the temporary file and delete the "build" folder in each directory
for /F "usebackq tokens=*" %i in (`type temp_dirs.txt`) do (
rd /s /q "%i\build"
echo Deleted "%i\build"
)
rem Delete the temporary file
del temp_dirs.txt
To use this script, copy and paste the code into a file called delete_build_folders.bat
and save it in the root directory of your Flutter project. Then, open a Command Prompt window, navigate to the root directory of your Flutter project, and run the script using the following command:
delete_build_folders.bat
This script will search for all directories that have a pubspec.yaml
file and delete the build
folder in each of those directories.
Note: This script uses the rd
command to delete the build
folders. This is a permanent operation and the files and folders in the build
folder will not be recoverable. Be sure to use this script carefully, and only if you are certain that you want to delete the build
folders in your Flutter projects.
That's all for now 🥳. If you have any questions, kindly leave a comment or connect with me on Twitter (Etornam Sunu)