47

How to delete the flutter packages in .pub-cache folder? When we give flutter clean, it will delete the build folder in the current directory. We can delete it manually, but my requirement is to delete the packages in .pub-cache folder using the command.

Dharanidharan
  • 1,038
  • 1
  • 9
  • 15

7 Answers7

67

To clear the global PUB_CACHE, run:

dart pub cache clean

or

flutter pub cache clean
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
35

If a dependency is removed from the pubspec and then pub get is run, it removes the dependency from the .packages file, making the dependency unavailable for importing.

If a packages in your pub cache to change or break, you can use flutter pub cache repair command performs a clean reinstall of all hosted and git packages in the system cache.

mboss
  • 123
  • 1
  • 8
victwise
  • 796
  • 5
  • 12
29

Short answer

Delete the pubspec.lock file then run the command flutter pub get again.

Long Story

  • the command flutter pub get will regenerate the pubspec.lock.
  • In case your code isn't tracked by a source code version control like git, make sure to take a backup copy from your pubspec.lock file before deleting it especially if the project is a bit old and you are using caret constraint for package depencedy and not using concrete versions in pubspec.ymal
  • if the project didn't compile after applying the answer it could be caret constraint issue so you have to update your project to match the new library version or use concrete versions in pubspec.ymal for example
dependencies:
  path: 1.3.0
Shady Mohamed Sherif
  • 15,003
  • 4
  • 45
  • 54
  • This file should be pushed to the source control, how should removing it be a good solution? – Mohammed Noureldin Jun 12 '21 at 18:25
  • The command `flutter pub get` will regenerate that file again, don't worry – Shady Mohamed Sherif Jun 13 '21 at 18:55
  • Then what is the purpose of checking this file to the source control? Something does not make sense in that. – Mohammed Noureldin Jun 13 '21 at 18:57
  • 1
    Keeping `pubspec.lock` in source control is optional but; It would be helpful if the project is a bit old and you are using `caret constraint` for package depencedy and not using `concrete versions` in `pubspec.ymal` which would cause other developers to get newer versions in the `pubspec.lock` which may cause compilation issues. – Shady Mohamed Sherif Jun 14 '21 at 01:13
  • Aah ok, that makes sense now, thanks Shady! But nevertheless, I may end up getting a newer version if I use the `caret`, which will encourage me to not remove this file :D – Mohammed Noureldin Jun 14 '21 at 08:20
  • 1
    You are welcome, Yes you are right; that's why you may need to keep a backup from your original `pubspec.lock` before you delete it if it isn't tracked by a source code version control :D I will add it as a note in the answer. – Shady Mohamed Sherif Jun 15 '21 at 17:10
14

Just go to the terminal and type the command

flutter pub remove package_name 

as like

flutter pub remove flutter_riverpod
m4n0
  • 29,823
  • 27
  • 76
  • 89
5

It took me more than 3 days to try all differently way. But finally I found, you need to go into the "pubspec.lock" file. Then go to the library and change the version there. Then go back to the file "pubspec.yaml" and run Packages get and it succeeds.

Doan Bui
  • 3,572
  • 25
  • 36
4

Opening a file explorer and deleting the .pub-cache directory in your home folder is pretty easy. Or you can use a normal command line command:

rm -r ~/.pub-cache

Next time you run

flutter pub get

This will redownload the packages specified in your project's pubspec.lock file. If you also delete pubspec.lock then flutter pub get will get the most recent non-breaking versions based on your pubspeck.yaml file.

Notes:

  • Any global Pub packages are also stored in the .pub-cache folder, so you'll have to install them again, too, whenever you need them next.
  • Depending on what you want to do, you probably don't need to delete .pub-cache. That's why there isn't a specific command for it. The only reason I can think of is if you want to save space by deleting the old versions of packages that you aren't using any more.
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
1

If you want to remove everything inside your cache folder to reclaim extra disk space or remove problematic packages:

flutter pub cache clean && flutter pub get 

To reinstall all packages in the system cache

flutter pub cache repair
Sharon Atim
  • 1,777
  • 16
  • 12