How to delete Nextcloud bookmarks using API

Delete Nextcloud bookmarks using API. Notice, data is paginated in this case, so this particar case is more interesting than regular add/remove operations.

Prerequisites

Install curl utility to perform API calls.

$ sudo apt-get install curl

Install jq utility to parse JSON files.

$ sudo apt-get install jq

Create application password

Create Nextcloud application password, do not use your regular credentials.

Delete Nextcloud bookmarks

Create nextcloud_bookmarks_del.sh shell script.

Shell script

#!/bin/bash
# Delete ALL Nextcloud bookmarks
# https://blog.sleeplessbeastie.eu/

# usage info
usage(){
  echo "Usage:"
  echo "  $0 -r nextcloud_url -u username -p passsword"
  echo ""
  echo "Parameters:"
  echo "  -r nextcloud_url   : set Nextcloud URL (required)"
  echo "  -u username        : set username (required)"
  echo "  -p password        : set password (required)"
  echo ""
}

# parse parameters
while getopts "r:u:p:f:t" option; do
  case $option in
    "r")
      param_nextcloud_address="${OPTARG}"
      param_nextcloud_address_defined=true
      ;;
    "u")
      param_username="${OPTARG}"
      param_username_defined=true
      ;;
    "p")
      param_password="${OPTARG}"
      param_password_defined=true
      ;;
    \?|:|*)
      usage
      exit
      ;;
  esac
done

if [ "${param_nextcloud_address_defined}" = true ] && \
   [ "${param_username_defined}"          = true ] && \
   [ "${param_password_defined}"          = true ]; then

  continue_pagination="1"
  while [ "${continue_pagination}" -eq "1" ]; do
    bookmarks=$(curl --silent --output - -X GET --user "${param_username}:${param_password}" --header "Accept: application/json" "${param_nextcloud_address}/index.php/apps/bookmarks/public/rest/v2/bookmark" | \
                jq -r '.data[].id')
    if [ -z "${bookmarks}" ]; then
      echo "Nextcloud bookmarks list is empty. Stopping."
      continue_pagination="0"
    else
      for bookmark in ${bookmarks}; do
        status=$(curl --silent -X DELETE --user "${param_username}:${param_password}"  "${param_nextcloud_address}/index.php/apps/bookmarks/public/rest/v2/bookmark/${bookmark}" | 
                 jq -r 'select(.status != "success") | .status')
        if [ -n "${status}" ]; then
          echo "There was an error when deleting Nextcloud bookmark id \"${bookmark}\". Stopping."
          exit 1
        else
          echo "Deleted Nextcloud bookmark id \"${bookmark}\""
        fi
      done
    fi
  done
else
  usage
fi

Usage

Display usage information.

nextcloud_bookmarks_del.sh 
Usage:
  ./nextcloud_bookmarks_del.sh -r nextcloud_url -u username -p passsword

Parameters:
  -r nextcloud_url   : set Nextcloud URL (required)
  -u username        : set username (required)
  -p password        : set password (required)

Delete bookmarks.

$ nextcloud_bookmarks_del.sh -r https://cloud.example.org/ -u milosz -p Mjdu3-kDnru-4UksA-fYs0w
Deleted Nextcloud bookmark id "735"
Deleted Nextcloud bookmark id "729"
Deleted Nextcloud bookmark id "734"
Deleted Nextcloud bookmark id "733"
Deleted Nextcloud bookmark id "732"
Deleted Nextcloud bookmark id "731"
Deleted Nextcloud bookmark id "730"
Deleted Nextcloud bookmark id "728"
Deleted Nextcloud bookmark id "727"
Deleted Nextcloud bookmark id "726"
Deleted Nextcloud bookmark id "724"
Deleted Nextcloud bookmark id "725"
Deleted Nextcloud bookmark id "723"
Deleted Nextcloud bookmark id "722"
Nextcloud bookmarks list is empty. Stopping.

Additional notes

Download source code.

Remember to backup Nextcloud bookmarks.

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.