65

How to clean react native project?

Is there any way to clean react native project as we can clean xcode project?

Any help will be appreciated!

Jagdeep Singh
  • 2,556
  • 3
  • 18
  • 28

12 Answers12

119

A react-native Project is about one XCode Project and one Android Project. (for pure js code, there's no need to do clean)

So, what you need would be

Clean Xcode Project with

cd ios
xcodebuild clean

And then clean Android Project with

cd android
./gradlew clean

You can simply write a batch file for it.

shim
  • 9,289
  • 12
  • 69
  • 108
Val
  • 21,938
  • 10
  • 68
  • 86
  • 1
    For what it's worth, 'xcodebuild clean' alone didn't work for my iOS project, where I am using a workspace file. In that case I had to use ... `xcodebuild clean -workspace [*.xcworkspace file] -scheme [project name]` If you don't know your file-name/scheme, just look at the first couple output lines in terminal, when running the normal build. It'll say something like: `info Found Xcode workspace "x.xcworkspace"` `info Building (using "xcodebuild -workspace [etc]` Second line, use -workspace & -scheme info from there. Cheers – hschmied May 18 '20 at 16:52
65

For android project

cd android &&./gradlew clean && cd ../

For iOS project

cd ios && xcodebuild clean && cd ../

If you need build clean again and again then add this scripts in the package.json file like this

scripts: { 
  "clean:android": "cd android && ./gradlew clean && cd ../",
  "clean:ios": "cd ios && xcodebuild clean && cd ../", 
}

Then run for android

yarn clean:android

And run for iOS

yarn clean:ios
dan-lee
  • 14,365
  • 5
  • 52
  • 77
Zeeshan Ansari
  • 9,657
  • 3
  • 26
  • 27
16

For Android

cd android
gradlew clean

For IOS

cd ios
xcodebuild clean
Sayo Babalola
  • 990
  • 14
  • 25
5

Delete node modules and

npm ci

react-native link

followed by

react-native start --reset-cache
spacedev
  • 1,086
  • 13
  • 13
4

this script will clean the Android and IOS build cache, then removes node modules and caches, and reinstall the react native project.

# rm -rf $HOME/.gradle/caches/
cd android && ./gradlew cleanBuildCache
cd ..
cd ios && pod cache clean --all && rm -rf build
cd ..
rm -rf node_modules
yarn cache clean --force
yarn install
cd ios && pod install

To reset the iOS simulator and erase all simulator data, focus on the simulator then go to Hardware -> Erase All Content and Settings

To reset the Android emulator and erase all data, open Android Studio then goes to menu Tools -> AVD Manager, then click the dropdown menu on the selected simulator and click on wipe data.

4

React Native clean-project is now the best and easiest way to do deal with react-native artifacts and cleanup. Since it's automatically detected by react-native, it becomes its own command in react-native.

$ yarn add -D react-native-clean-project
$ react-native clean-project
DBrown
  • 5,111
  • 2
  • 23
  • 24
4

More useful cleaner scripts. Seperating modules cleaning & native cleaning, based on the other great answers:

"scripts": {
      "npm:clean": "rm -rf node_modules && npm cache clean && npm install"
      "yarn:clean": "rm -rf node_modules && yarn cache clean --force && yarn install"
      "android:clean": "cd android && ./gradlew clean && ./gradlew cleanBuildCache && cd ..",
      "ios:clean": "cd ios && xcodebuild clean && rm -rf ~/Library/Caches/CocoaPods && rm -rf Pods && rm -rf ~/Library/Developer/Xcode/DerivedData/* && pod cache clean --all && pod deintegrate && pod setup && pod install && cd ..",
    }
2

To reset react-native specific cache, run:

npm start -- --reset-cache

if in package.json you have

"scripts": {
  "start": "node node_modules/react-native/local-cli/cli.js start"
}
jughosta
  • 121
  • 7
2

I added the following scripts to my package.json

scripts: {
  ....
  "clean:android": "echo 'require(\"child_process\").execSync(\"cd android; ./gradlew clean\", {stdio: \"inherit\"})' | node ",
  "clean:ios": "echo 'require(\"child_process\").execSync(\"cd ios; xcodebuild clean\", {stdio: \"inherit\"})' | node ",
  "clean": "npm run clean:android && npm run clean:ios",
  ....
}

now you can clean android

npm run clean:android

clean ios

npm run clean:ios

clean both

npm run clean
KitKat23
  • 29
  • 3
0

React and node specific cleaning:

Remove node_modules to avoid inconsitence which may appear somehow rarely:

rm -rf node_modules

(reinstall if need with npm or yarn)

Clean npm cache if npm is used

npm cache clean

Clean react temp files:

rm -rf $TMP/react*
oklas
  • 7,935
  • 2
  • 26
  • 42
0

Clean the Android Project with:

cd android/
gradlew clean
Blue
  • 22,608
  • 7
  • 62
  • 92
Priyanga Manivelan
  • 432
  • 1
  • 5
  • 14
-15

remove android folder run

react-native upgrade

then

react-native run-android
bro
  • 165
  • 1
  • 8