Explain Codes LogoExplain Codes Logo

How to clear react-native cache?

javascript
cache-management
react-native
npm-cache
Anton ShumikhinbyAnton Shumikhin·Dec 10, 2024
TLDR

Without further ado, here's how to quickly and efficiently zap your React Native cache:

  1. Reset metro cache:

    # This is the big red button npx react-native start --reset-cache
  2. Clear watchman artifact:

    # Watchman's worst nightmare watchman watch-del-all
  3. Refresh dependencies:

    # Time for a fresh start! rm -rf node_modules && npm install
  4. Clean iOS build folder:

    # A trip to the launderette for iOS (cd ios && xcodebuild clean)
  5. Clean Android Gradle project:

    # Let's give Gradle a bath! (cd android && ./gradlew clean)

If you're using Expo:

# Expo's washing machine expo start -c

Shake up your Expo environment with this command.

Deep dive into cache clearing

Mastering npm cache

When the usual tools fail to do the trick, npm's cache could be the hidden suspect. A force clean is then the way to go:

# As subtle as a sledgehammer npm cache clean --force

While this option eliminates all npm goodies, it should not be your first resort due to its broad brushstroke effect.

Maintaining npm and React Native versions

Keeping your npm and React Native versions in sync helps to stave off version conflicts. The package.json file can be your compass, assisting in navigating the compatibility maze.

A deeper cleanse

Looking for a thorough cleanse? Consider clearing temporary directories associated with your React Native project:

# The software equivalent of washing your hands rm -rf $TMPDIR/react-*

This helps to eradicate stubborn storage locations lingering with outdated information.

Handling your cache like a pro

Tailored treatments for iOS and Android

For an environmentally sound approach in clearing cache, considering the specific Operating Systems is beneficial:

For iOS Additional to performing xcodebuild clean, clearing Derived Data can bring you closer to the solution:

# Goodbye old data, hello new beginnings! rm -rf ~/Library/Developer/Xcode/DerivedData

For Android With caches residing in various nooks and corners, apart from executing ./gradlew clean:

  • Clear the build cache:
    # Giving Android a thorough spring cleaning ./gradlew cleanBuildCache
  • Remove the Gradle wrapper cache directory:
    # Because wrappers can get dirty too! rm -rf ~/.gradle/wrapper/dists

Cache control in network operations

Network caching issues, such as image caching, can be handled by:

  • Resetting Network Settings on your device or simulator.
  • Using libraries (such as axios) to set cache-control headers on your network requests.