Clearing Flutter’s build cache can be useful if you’re experiencing issues with your builds or just want to ensure a clean build environment. There are multiple ways to do it properly. Here’s how you can do it:

1. Clean the Project

Flutter provides a built-in command to clean the project, which can help resolve many issues related to the build cache:
flutter clean

This command deletes the build/ and .dart_tool/ directories in your Flutter project, which forces Flutter to rebuild everything the next time you run the app.

2. Delete the build Directory Manually

If you want to manually delete the build cache, you can do the following:

  1. Navigate to your Flutter project directory.
  2. Delete the build/ directory.

Here is the command to do it:
rm -rf build/

3. Delete the pubspec.lock File

Sometimes, issues can be resolved by deleting the pubspec.lock file and then running flutter pub get to regenerate it.

Here is the command to do it:
rm pubspec.lock
flutter pub get

4. Invalidate Caches in Android Studio

If you’re using Android Studio or IntelliJ IDEA, you can invalidate the caches:

  • Go to File -> Invalidate Caches / Restart….
  • Click on Invalidate and Restart.

This will clear the IDE caches and restart the IDE.

5. Clear the Gradle Cache

For Android-specific issues, you might need to clear the Gradle cache. This can be done by deleting the .gradle directory:

Here is the command to do it:
rm -rf ~/.gradle

Or by deleting the .gradle directory in your project’s android/ directory:

Here is the command to do it:
cd android
./gradlew clean

Summary

  • Use flutter clean to clear the build cache.
  • Manually delete the build/ directory.
  • Delete the pubspec.lock file and run flutter pub get.
  • Invalidate caches in Android Studio.
  • Clear the Gradle cache if necessary.

These steps should help resolve build-related issues by ensuring a clean build environment.

Support On Demand!

Flutter