How to Use Pacman in Arch Linux

March 23, 2023

The pacman is a package manager for Arch Linux-based distros (ex - Arch Linux, Manjaro, ArcoLinux, Artix Linux, Garuda Linux, ArcoLinux, Archlabs Linux, etc.). It installs, removes, updates, and everything else related to your packages. It is a very powerful package manager. AUR helpers like yay also use pacman for installation and other related operations.

In this article, I will talk about pacman's configuration, how to use it to install and remove any package, how to update/downgrade a package, upgrade your Arch Linux-based distros. Finally, I will give you a very useful and simple script that will handle all such operations.

Configuration of pacman

We use the file /etc/pacman.conf to configure the pacman. All the settings are mentioned in the file with their full description. Open it using your favorite text editor such as nvim and uncomment the options you want.

$ sudo nvim /etc/pacman.conf

Currently, I use the following configuration in it:

# See the pacman.conf(5) manpage for option and repository directives

#
# GENERAL OPTIONS
#
[options]
# The following paths are commented out with their default values listed.
# If you wish to use different paths, uncomment and update the paths.
#RootDir     = /
#DBPath      = /var/lib/pacman/
#CacheDir    = /var/cache/pacman/pkg/
#GPGDir      = /etc/pacman.d/gnupg/
#HookDir     = /etc/pacman.d/hooks/
HoldPkg     = pacman glibc
#XferCommand = /usr/bin/curl -L -C - -f -o %o %u
#XferCommand = /usr/bin/wget --passive-ftp -c -O %o %u
#CleanMethod = KeepInstalled
Architecture = auto

# Pacman won't upgrade packages listed in IgnorePkg and members of IgnoreGroup
#IgnorePkg   =
#IgnoreGroup =

#NoUpgrade   =
#NoExtract   =

# Misc options
#UseSyslog
Color
#NoProgressBar
CheckSpace
VerbosePkgLists
ParallelDownloads = 5

# By default, pacman accepts packages signed by keys that its local keyring
# trusts (see pacman-key and its man page), as well as unsigned packages.
SigLevel    = Required DatabaseOptional
LocalFileSigLevel = Optional
#RemoteFileSigLevel = Required

# NOTE: You must run `pacman-key --init` before first using pacman; the local
# keyring can then be populated with the keys of all official Arch Linux
# packagers with `pacman-key --populate archlinux`.

#
# REPOSITORIES
#   - can be defined here or included from another file
#   - pacman will search repositories in the order defined here
#   - local/custom mirrors can be added here or in separate files
#   - repositories listed first will take precedence when packages
#     have identical names, regardless of version number
#   - URLs will have $repo replaced by the name of the current repo
#   - URLs will have $arch replaced by the name of the architecture
#
# Repository entries are of the format:
#       [repo-name]
#       Server = ServerName
#       Include = IncludePath
#
# The header [repo-name] is crucial - it must be present and
# uncommented to enable the repo.
#

# The testing repositories are disabled by default. To enable, uncomment the
# repo name header and Include lines. You can add preferred servers immediately
# after the header, and they will be used before the default mirrors.

#[testing]
#Include = /etc/pacman.d/mirrorlist

[core]
Include = /etc/pacman.d/mirrorlist

[extra]
Include = /etc/pacman.d/mirrorlist

#[community-testing]
#Include = /etc/pacman.d/mirrorlist

[community]
Include = /etc/pacman.d/mirrorlist

# If you want to run 32 bit applications on your x86_64 system,
# enable the multilib repositories as required here.

#[multilib-testing]
#Include = /etc/pacman.d/mirrorlist

[multilib]
Include = /etc/pacman.d/mirrorlist

# An example of a custom package repository.  See the pacman manpage for
# tips on creating your own repositories.
#[custom]
#SigLevel = Optional TrustAll
#Server = file:///home/custompkgs

Many of the above options were already uncommented. I have uncommented the following options:

  • Color: gives colored output

colored output in pacman

Figure: Colored output in pacman

  • VerbosePkgLists: makes a good-looking table with -> to showcase changes. See the above image.

  • ParallelDownloads = 5: by default, pacman downloads only one package during installation/upgrade/update. Using this option forces pacman to download 5 packages at a time. Thus it increases the download speed.

Other options might be important to you:

  • IgnorePkg = package1 package2: Now, these packages package1 and package2 will be ignored by pacman. That means pacman will not update them with the system upgrade command Pacman -Syu. Put a list of all the packages you don't want to update. And do not forget the spaces in between them. This is helpful when updating some package is breaking it. This might be the case with bleeding-edge distros like Arch Linux which updates very often. However, in my whole life, I never needed to use this 😁.

Repositories in Arch Linux

As you can see in pacman configuration file /etc/pacman.conf, there are four official repositories

  • core: fundamental Linux packages like curl, bash, etc. are found here.

  • extra: Most mainstream GUI packages like firefox, gnome-desktop, etc. can be found here.

  • community: the biggest official repository. If you are a developer and want your package to get included in the official Arch Linux repository, it is highly likely your package will end up here. Example - ranger, i3-gaps

  • multilib: 32-bit packages can be found here. Gamers find it very useful since packages like Wine, and Steam, are located here.

Arch User Repository (AUR) on the other hand is not the official repository (repo). Anyone can upload his/her packages in the AUR. Packages in the AUR are not checked as rigorously as those in the official repository for malware, system breakage, and other problems. Despite these drawbacks, it is very important since packages from the AUR are chosen very often to get into the official repositories. On top of that, many famous packages like Google Chrome are only available in this. So, use this repository only if you trust the package.

So, whenever you install any app, try your best to stick with the packages from these repositories. When you upgrade your system, these apps along with their dependencies will also be updated with the upgrade command pacman -Syu. This will cause fewer problems in your system and packages. And if you cannot find your package in the official repository, then prefer AUR to GitHub and other sources because AUR installs all the dependencies, and uninstallation of packages is very easy - just use the pacman.

So, overall preference of repositories:

core > extra > community > AUR > GitHub and others

How to install packages using pacman in Arch Linux

To install a package myapp use the following command:

$ sudo pacman -S myapp

Example:

$ sudo pacman -S firefox

Use the flag --noconfirm if you don't want to write y each time you install something. Similarly, use --needed to install only the needed packages i.e. do not install the packages which are already installed and updated.

$ sudo pacman -S firefox --noconfirm --needed

How to upgrade pacman database

Pacman keeps a list of all packages and related information (like version, dependency, etc.) in a special database. You need to periodically update this database.

For this execute the following command

$ sudo pacman -Sy

or

$ sudo pacman -S -y

You can create a cronjob for this. Open the file /etc/crontab using your text editor and add the following line. Change the time and/or day according to your need.

$ sudo nvim /etc/crontab

# pacman's local database update at 06:48 daily
48 6 * * *  root pacman -Sy

You can create an anacron job if your computer is off very often. Use the file /etc/anacrontab. To learn more about how to create an anacron job, read my article on it.

$ sudo nvim /etc/anacrontab

# pacman's local database update at every 1st day
1  0  pacman_database pacman -Sy 

How to upgrade Arch Linux

To upgrade your system use the pacman -Su command.

$ sudo pacman -Su

Or,

$ sudo pacman -S -u

It will update all the packages and their dependencies.

However, using -Syu (aka -S -y -u) will be better since that will update the database as well. Now, you will get the latest packages.

$ sudo pacman -Syu

How to use pacman to learn about installed packages in Arch Linux

The -Q operation in the pacman and all its associated flags give you information about the local packages. Using this, you can find files, and dependencies brought by the package, whether the package has been installed as a dependency or explicitly by you, whether the package is part of a group, and so on.

How to get all information about installed packages

To get all information about any installed packages use the -Qi:

$ pacman -Qi package_name

Example:

$ pacman -Qi dconf
Name            : dconf
Version         : 0.38.0-1
Description     : Configuration database system
Architecture    : x86_64
URL             : https://wiki.gnome.org/Projects/dconf
Licenses        : LGPL
Groups          : None
Provides        : libdconf.so=1-64
Depends On      : glib2
Optional Deps   : None
Required By     : colord  gsettings-desktop-schemas  gtk3  gvfs
Optional For    : None
Conflicts With  : None
Replaces        : None
Installed Size  : 443.25 KiB
Packager        : Jan Alexander Steffens (heftig) <heftig@archlinux.org>
Build Date      : Thu 17 Sep 2020 11:58:07 PM IST
Install Date    : Mon 23 Nov 2020 08:31:39 PM IST
Install Reason  : Installed as a dependency for another package
Install Script  : Yes
Validated By    : Signature

Note 1: Similar flag is also available with -S. -Qi is all about 'your' system

$ pacman -Si dconf

Repository      : extra
Name            : dconf
Version         : 0.40.0-2
Description     : Configuration database system
Architecture    : x86_64
URL             : https://wiki.gnome.org/Projects/dconf
Licenses        : LGPL
Groups          : None
Provides        : libdconf.so=1-64
Depends On      : glib2
Optional Deps   : None
Conflicts With  : None
Replaces        : None
Download Size   : 105.75 KiB
Installed Size  : 457.01 KiB
Packager        : Jan Alexander Steffens (heftig) <heftig@archlinux.org>
Build Date      : Thu 17 Nov 2022 11:08:58 PM IST
Validated By    : MD5 Sum  SHA-256 Sum  Signature

Note 2: The result in pacman -Qi package_name is a little different from that in pacman -Si package_name. For example, look at the line starting with Required by. This line tells which packages are using it as a dependency in 'your' system.

How to find explicitly installed packages in Arch Linux

To find explicitly installed packages, use the flag -Qe.

$ pacman -Qe

Output:

accountsservice 22.08.8-4
alsa-utils 1.2.8-1
amd-ucode 20230210.bf4115c-1
ani-cli-git r662.ec28d2a-1
anki 2.1.60-1
atool 0.39.0-8
autoconf 2.71-4
automake 1.16.5-2
...

😁 Fun fact: It is helpful when you want to reinstall your Linux system. Just reinstall Arch Linux, and then reinstall the explicitly installed packages. Copy their configuration and you are done. Recall the amount of time you put into Windows for such an operation.

Extra point: To find the explicitly installed groups, execute the -Qge (g for group, e for explicit):

$ pacman -Qge

Search for installed packages using regular expression in Arch Linux

The following pacman -Qs command searches the packages using the given regular expression.

pacman -Qs regex

Or,

pacman -Q -s regex

How to find packages in need of an update in Arch Linux

To find the packages which are needed to be updated use the flag -Qu (u for update). But don't forget to update your database before that.

$ sudo pacman -Sy

Now, execute the following command:

$ sudo pacman -Qu

Example Output:

$ pacman -Qu 

bazel 6.0.0-2 -> 6.1.1-1
coreutils 9.1-3 -> 9.2-1
cryptsetup 2.6.1-1 -> 2.6.1-3
...

The above output might also show the list of packages that are included in IgnorePkg. Pacman will put [ignored] next to their name and will not update them on pacman -Syu

How to use pacman to list orphan packages

Orphan packages are those packages that were installed as dependencies; however, the package installed as a dependency is no longer used. Hence, these packages are no longer needed and can be removed.

To list orphan packages, execute the command pacman -Qdtq.

Explanation:

-d: To list dependency packages
-t: To list unrequired packages and hence needs to be terminated.
-q: Quiet output

Sample Output:

$ pacman -Qdtq

bazel
db
go
ninja
python-appdirs
python-installer
python-pyaudio
python-pyparsing
rust
ucl
upx
webkit2gtk-4.1

However, take a system backup before removing such packages. Restore your backup if any problem arises.

How to remove packages using pacman in Arch Linux

For removal-related operations, we use the pacman flag -R.

$ sudo pacman -R package_name

The above command removes the package with the name package_name. It is simplest since it does not remove any of the package's dependencies and its configuration files.

Remove packages and their unneeded dependencies

To remove a package and all of its unneeded dependencies, use -Rsu.

When a target package is a group (like xorg), and one of the packages of the group is used by another package, you need to exclude such package. Here, use -Rsu to instruct pacman to omit such dependencies and remove the rest ones. It will also exclude the explicitly installed packages.

$ sudo pacman -Rsu package_name

For example, if your output contains the following:

...
warning: removing cairo from target list
...

It means here cairo is being removed from the target list because it is being used by other packages. cairo will not be uninstalled.

Clean removal of a package

When pacman removes any package, it keeps its configuration files (with .pacsave extension). -n instructs it to remove these as well (-n means nosave) Ex - /etc/updatedb.conf removed when the package mlocate is removed.

$ sudo pacman -Rsun package_name

Use this for cleaner uninstallation.

📝 Note: A very dangerous command is -Rsc. It removes the given package, its dependencies (-s) and other packages depending upon this (-c). So avoid this.

$ sudo pacman -Rsc package_name

How to downgrade a package using pacman in Arch Linux

Sometimes, upgrading your Arch Linux-based system might break a few packages. In that case, you might want to downgrade them until the package developer fixes the problem. Downgrade the package and use the IgnorePkg option mentioned above to prevent pacman from updating them.

To downgrade the package you want, first look for it in the directory /var/cache/pacman/pkg. In this directory, pacman keeps all the packages in the form of *package_version.pkg.tar.zst before installing it.

The packages in my system are:

$ ls -1 /var/cache/pacman/pkg

accountsservice-22.08.8-2-x86_64.pkg.tar.zst
accountsservice-22.08.8-2-x86_64.pkg.tar.zst.sig
accountsservice-22.08.8-4-x86_64.pkg.tar.zst
accountsservice-22.08.8-4-x86_64.pkg.tar.zst.sig
acl-2.3.1-3-x86_64.pkg.tar.zst
acl-2.3.1-3-x86_64.pkg.tar.zst.sig
adobe-source-code-pro-fonts-2.038ro+1.058it+1.018var-1-any.pkg.tar.zst
adobe-source-code-pro-fonts-2.038ro+1.058it+1.018var-1-any.pkg.tar.zst.sig
adwaita-cursors-43-2-any.pkg.tar.zst
adwaita-cursors-43-2-any.pkg.tar.zst.sig
...

Now, find the package and install it using pacman -U command:

$ sudo pacman -U <your_package>

Example,

$ sudo pacman -U /var/cache/pacman/pkg/lua-5.3.5-3-x86_64.pkg.tag.xz

Logs in the pacman command in Arch Linux

Pacman command puts its output in a logfile called pacman.log. The file is located in the standard log directory /var/log. You can use this file to find all the commands pacman executed in the past.

Here are a few lines from my log file:

[2020-11-25T15:16:24+0530] [PACMAN] Running 'pacman -S alacritty'
[2020-11-25T15:16:27+0530] [ALPM] transaction started
[2020-11-25T15:16:27+0530] [ALPM] installed alacritty (0.5.0-3)
[2020-11-25T15:16:27+0530] [ALPM] transaction completed
[2020-11-25T15:16:27+0530] [ALPM] running '30-systemd-update.hook'...
[2020-11-25T15:16:27+0530] [ALPM] running 'update-desktop-database.hook'...
[2020-11-25T15:20:13+0530] [PACMAN] Running 'pacman -R alacritty'
[2020-11-25T15:20:18+0530] [ALPM] transaction started
[2020-11-25T15:20:18+0530] [ALPM] removed alacritty (0.5.0-3)
[2020-11-25T15:20:18+0530] [ALPM] transaction completed
[2020-11-25T15:20:18+0530] [ALPM] running '30-systemd-update.hook'...
[2020-11-25T15:20:18+0530] [ALPM] running 'update-desktop-database.hook'...
[2020-11-25T23:11:27+0530] [PACMAN] Running 'pacman -S w3m'

In the above output, the [PACMAN] flag is given to explicit pacman commands. You can use this to find explicitly installed packages.

Troubleshooting: problems and solutions

Here are the few errors I have faced:

signature from … is marginal trust

Once upon a time, I got an error signature from "David Runge <dvzrv@archlinux.org>" is marginal trust. If you get something similar then you are in the same place.

If your system upgrade gets delayed for an extended period, you get this error, while trying to update.

Solution: Sync the package database using pacman -Sy and upgrade the package archlinux-keyring. Now, you can upgrade your system. One line command for this:

$ sudo pacman -Sy archlinux-keyring && pacman -Su

pacman failed to init transaction (unable to lock database)

When pacman is trying to change the local database (by updating it), it locks it by creating a file /var/lib/pacman/db.lck. This is done so that other pacman commands cannot be executed at this time. After the pacman has executed its command, the *.lck file is removed.

However, when the pacman command is interrupted, the .lck file is not removed and from next time pacman throws the error.

Solution:

  1. You need to remove this file using rm /var/lib/pacman/db.lck, or
  2. Wait for other pacman commands, if any, to get finished.

How to look for files inside a package

To look for files inside a package, use -F.

Syntax:

pacman -F <file_name>

For example, to find out which package provides you urxvt binary, execute the following command:

$ pacman -F urxvt

Output:
community/rxvt-unicode 9.31-1 [installed]
    usr/bin/urxvt

You can use -l with -F to list all files of a package.

Syntax:

pacman -Fl <package_name>

For example, to list all files of papirus-icon-theme, execute:

$ pacman -Fl papirus-icon-theme

papirus-icon-theme usr/
papirus-icon-theme usr/share/
papirus-icon-theme usr/share/icons/
papirus-icon-theme usr/share/icons/Papirus-Dark/
papirus-icon-theme usr/share/icons/Papirus-Dark/128x128
papirus-icon-theme usr/share/icons/Papirus-Dark/16x16/
papirus-icon-theme usr/share/icons/Papirus-Dark/16x16/actions/
papirus-icon-theme usr/share/icons/Papirus-Dark/16x16/actions/Finished.svg
papirus-icon-theme usr/share/icons/Papirus-Dark/16x16/actions/Info-amarok.svg
papirus-icon-theme usr/share/icons/Papirus-Dark/16x16/actions/SuggestionError.svg
...

Icing on the cake: pactree and pacdiff

For them, first install the package pacman-contrib.

sudo pacman -S pacman-contrib

This package has been created in order to debloat pacman. Earlier, pacman provided this functionality but not now.

This package provides many commands related to pacman:

$ pacman -Fl pacman-contrib | grep bin   
pacman-contrib usr/bin/
pacman-contrib usr/bin/checkupdates
pacman-contrib usr/bin/paccache
pacman-contrib usr/bin/pacdiff
pacman-contrib usr/bin/paclist
pacman-contrib usr/bin/paclog-pkglist
pacman-contrib usr/bin/pacscripts
pacman-contrib usr/bin/pacsearch
pacman-contrib usr/bin/pacsort
pacman-contrib usr/bin/pactree
pacman-contrib usr/bin/rankmirrors
pacman-contrib usr/bin/updpkgsums

I am going to describe two of them.

pactree:-

To find the list of all dependencies of a package, use pactree. For example, to get the list of all dependencies of deja-dup package, execute the command:

$ sudo pactree deja-dup

pacdiff:-

The script pacdiff looks for *.pacorig, *.pacnew and *.pacsave files. These files are created during various pacman operations. For example, when you upgrade your packages and if any of the packages bring new configuration file(s), these new files are saved as *.pacnew. You are supposed to copy all the new diffs from here into your original configuration file and then delete it. You can merge these diffs manually or use the pacdiff command with appropriate flags. Look at the man page man 8 pacdiff for such flags.

To get the list of all such files, execute the following command:

$ pacdiff -o

pacman is eating up your disk space

As above mentioned, pacman stores all packages in the directory /var/cache/pacman/pkg before installing them. Over time, this starts to take up a lot of space. Similarly, its sync database also takes a lot of space.

So you need to delete these unneeded packages from the cache directory along with unused sync databases to free up disk space. For that, execute the command:

$ sudo pacman -Sc

Use two -c's to remove all files from the cache.

📝 Note: paccache also have similar abilities.

A handy script for all of the above operations

Now, I will give you a simple and handy script comprising all of the above commands and options.

Learn here about what is a shell script and how to create one.

You don't have to remember anything now. It also includes operations on AUR packages using yay. Comment/uncomment out appropriate lines as given in the script if you don't want to use yay.

#!/bin/bash

FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS --preview-window=80%"

set -o errexit # exit on error
set -o pipefail # fail the pipe if any of its command fails
set -o nounset # if any variable is undefined exit

find_internet_install(){

  pacman -Slq | 
    fzf -m --preview 'cat <(pacman -Si {1}) <(pacman -Fl {1} | awk "{print \$2}")' |
    xargs -ro sudo pacman -S
}  


find_aur_install(){

  yay -Slq | 
    fzf -m --preview 'cat <(yay -Si {1}) <(yay --getpkgbuild --print {1})' |
    xargs -ro yay -S

}  


find_local_remove(){

  pacman -Qq | fzf --multi --preview 'pacman -Qi {1}' | xargs -ro sudo pacman -Rsun

}


remove_orphans(){

  echo -e "pacman -Rsun:- Remove all unneeded (-u) dependencies (-s):\n"
  sudo pacman -Rsun $(pacman -Qdtq) 

}

database_update(){
  sudo pacman -Sy 1> /dev/null 2> /dev/null
}


system_upgrade(){

  echo "System Upgrade:" &&
  # uncomment the pacman line given below if you don't use AUR (yay); don't forget to comment out the yay line
  # sudo pacman -Syu && 
  yay -Syu --combinedupgrade --devel --batchinstall --sudoflags "--askpass" && 
  echo "PACDIFF..." && 
  pacdiff -o

}

empty_cache(){

  # remove unused cached database and packages.
  echo "Empty Unused Caches:"
  yay -Sc
  # comment out the above yay line and uncomment the following pacman line if you don't use AUR and yay
  # sudo pacman -Sc

}


help_page(){

cat << document
-i get from the [i]nternet repositories (official arch repos)
-a get from the [a]ur
-l remove from the [l]ocal repositories 
-r [r]emove orphan packages
-s [s]ystem upgrade
-d [d]atabase update
-e [e]mpty unused cached databases and packages.
-h print this [h]elp
document

}


main(){

  case "$1" in
    "-i") find_internet_install ;;
    "-a") find_aur_install ;;
    "-l") find_local_remove ;;
    "-r") remove_orphans ;;
    "-s") system_upgrade ;;
    "-d") database_update ;;
    "-e") empty_cache ;;
    "-h") help_page ;;
    *)    help_page && exit 1 ;;
  esac

}


main "$1"

Save the above script in a file with a name pcm.sh in your $PATH directory. Set the appropriate execution bit.

Now, you can run pcm.sh -h to find all available options:

 pcm.sh -h             
-i get from the [i]nternet repositories (official arch repos)
-a get from the [a]ur
-l remove from the [l]ocal repositories 
-r [r]emove orphan packages
-s [s]ystem upgrade
-d [d]atabase update
-e [e]mpty unused cached databases and packages.
-h print this [h]elp

The script in action:

Install a package

$ pcm.sh -i

using fzf with pacman to install packages

Figure: using fzf with pacman to install packages

Remove orphan packages

$ pcm.sh -r

Output:

pacman -Rsun:- Remove all unneeded (-u) dependencies (-s):

[sudo] password for ajay: 
checking dependencies...
:: atool optionally requires zip: support creating zip archives
:: libbluray optionally requires java-runtime: BD-J library
:: libjxl optionally requires java-runtime: for JNI bindings
:: libreoffice-fresh optionally requires java-runtime: adds java support
:: libreoffice-fresh optionally requires java-environment: required by extension-wiki-publisher and extension-nlpsolver

Package (19)             Old Version    Net Change 

java-environment-common  3-5               0.00 MiB
java-runtime-common      3-5              -0.01 MiB
jdk11-openjdk            11.0.18.u10-2  -322.23 MiB
jre11-openjdk            11.0.18.u10-2    -0.52 MiB
jre11-openjdk-headless   11.0.18.u10-2  -159.78 MiB
libnet                   1:1.1.6-1        -0.30 MiB
zip                      3.0-10           -0.53 MiB
bazel                    6.0.0-2        -110.26 MiB
db                       6.2.32-1         -7.16 MiB
go                       2:1.20.2-1     -195.91 MiB
ninja                    1.11.1-2         -0.35 MiB
python-appdirs           1.4.4-7          -0.07 MiB
python-installer         0.7.0-1          -0.77 MiB
python-pyaudio           0.2.12-1         -0.14 MiB
python-pyparsing         3.0.9-1          -0.96 MiB
rust                     1:1.68.0-1     -516.04 MiB
ucl                      1.03-9           -0.10 MiB
upx                      4.0.2-1          -2.21 MiB
webkit2gtk-4.1           2.40.0-2        -90.94 MiB

Total Removed Size:  1408.28 MiB

:: Do you want to remove these packages? [Y/n] 

Clean up

$ pcm.sh -e

Output:

Packages to keep:
  All locally installed packages

Cache directory: /var/cache/pacman/pkg/
:: Do you want to remove all other packages from cache? [Y/n] n

Database directory: /var/lib/pacman/
:: Do you want to remove unused repositories? [Y/n] n

Build directory: /home/ajay/.cache/yay
:: Do you want to remove all other AUR packages from cache? [Y/n] n
:: Do you want to remove ALL untracked AUR files? [Y/n] n

Check out other options available in the script.

Conclusion

That's all folks. In short, just create a cronjob/anacronjob for database update. Use the script for installation/removal/update/others. If you have any suggestions/questions, use the comment section below. To learn more, look at pacman's man pages man pacman, man pacman.conf, man pages for binaries pacdiff, pacsearch, pactree, etc, and yay's man page at man yay. Another point to note that most of these commands also work with yay. So don't forget to look at my article on AUR helper yay.