Skip to content

Instantly share code, notes, and snippets.

@corona6
Last active November 11, 2021 14:56
Show Gist options
  • Save corona6/509264be060015d293c8d292e4637a72 to your computer and use it in GitHub Desktop.
Save corona6/509264be060015d293c8d292e4637a72 to your computer and use it in GitHub Desktop.
delete unused resources/revisions/tags for Joplin https://github.com/corona6/joplinclean

MOVED

https://github.com/corona6/joplinclean

Install

curl -O https://gist.githubusercontent.com/corona6/509264be060015d293c8d292e4637a72/raw/joplin_clean_up.sh
chmod +x joplin_clean_up.sh

Usage

Usage:
  ./joplin_clean_up.sh [-frtyh] path

Examples:
  # delete image or file that not used in the note
  ./joplin_clean_up.sh /path/to/synchronise/directory

  # delete the history (revisions) of the note which are already deleted
  ./joplin_clean_up.sh -r /path/to/synchronise/directory

  # delete the tags that not link to any notes
  ./joplin_clean_up.sh -t /path/to/synchronise/directory

Options:
  -f, --force         Delete unused resources even if it's in the revision
  -r, --revision      Delete unused revisions
  -t, --tag           Delete unused tags
  -y, --yes           Automatic yes to all prompts
  -h, --help          Print this help

Example

./joplin_clean_up.sh /Users/corona6/joplin

#!/usr/bin/env bash
show_help() {
cat << EOF
Usage:
$0 [-frtyh] path
Examples:
# delete image or file that not used in the note
$0 /path/to/synchronise/directory
# delete the history (revisions) of the note which are already deleted
$0 -r /path/to/synchronise/directory
# delete the tags that not link to any notes
$0 -t /path/to/synchronise/directory
Options:
-f, --force Delete unused resources even if it's in the revision
-r, --revision Delete unused revisions
-t, --tag Delete unused tags
-y, --yes Automatic yes to all prompts
-h, --help Print this help
EOF
}
cleanup() {
rm "$joplin_path/$1.md"
rm "$joplin_path/.resource/$1"
echo -n "deleted $joplin_path/.resource/$1"
echo
}
delete_file() {
rm "$1"
echo -n "deleted $1"
echo
}
ARGS=""
while [ $# -gt 0 ]
do
unset OPTIND
unset OPTARG
while getopts ":hfrty-:" opt; do
if [ "$opt" = "-" ]; then
opt=`echo ${OPTARG}`
fi
case "$opt" in
h | help)
show_help
exit 0
;;
f | force)
force=true
;;
r | revision)
revision=true
;;
t | tag)
tag=true
;;
y | yes)
skip_prompt=true
;;
esac
done
shift $((OPTIND-1))
ARGS="${ARGS}$1"
shift
done
if [ "$ARGS" = "" ]; then
show_help
exit 1
fi
joplin_path=$ARGS
if [ $revision ]; then
item_ids=($(grep -lr -E "^type_: 13$" ${joplin_path} | xargs grep -E "^item_id:" | awk '{ print $2 }' | sort | uniq))
for item_id in ${item_ids[@]}; do
if [ ! -e "$joplin_path/$item_id.md" ]; then
revision_files=($(grep -lr -E "^item_id: $item_id$" ${joplin_path}))
for revision_file in ${revision_files[@]}; do
if [ ! $skip_prompt ]; then
read -p "delete $revision_file ? (y/n) " answer
if [ ! $answer = "y" ]; then
continue
fi
fi
delete_file $revision_file
done
fi
done
elif [ $tag ]; then
tag_links=($(grep -lr -E "^type_: 6$" ${joplin_path} | xargs grep -E "^note_id:" | awk '{ print $1 $2 }'))
for tag_link in ${tag_links[@]}; do
note_id=($(echo ${tag_link} | awk -F ':' '{ print $3 }'))
if [ ! -e "$joplin_path/$note_id.md" ]; then
tag_link_file=($(echo ${tag_link} | awk -F ':' '{ print $1 }'))
if [ ! $skip_prompt ]; then
read -p "delete $tag_link_file ? (y/n) " answer
if [ ! $answer = "y" ]; then
continue
fi
fi
delete_file $tag_link_file
fi
done
tag_files=($(grep -lr -E "^type_: 5$" ${joplin_path}))
for tag_file in ${tag_files[@]}; do
file_name=${tag_file##*/}
tag_id=${file_name%.md}
check=$(grep -lr -E "^tag_id: $tag_id$" ${joplin_path} | wc -l)
if [ $check -eq 0 ]; then
if [ ! $skip_prompt ]; then
read -p "delete $tag_file ? (y/n) " answer
if [ ! $answer = "y" ]; then
continue
fi
fi
delete_file $tag_file
fi
done
else
resources=($(ls ${joplin_path}/.resource))
for resource in ${resources[@]}; do
if [ $force ]; then
check=$(grep -lr ${resource} ${joplin_path} | xargs grep -E "^type_: 1$" | wc -l)
else
check=$(grep -lr ${resource} ${joplin_path} | wc -l)
((check=check-1))
fi
if [ $check -eq 0 ]; then
if [ ! $skip_prompt ]; then
read -p "delete $joplin_path/.resource/$resource ? (y/n) " answer
if [ ! $answer = "y" ]; then
continue
fi
fi
cleanup $resource
fi
done
fi
@heartnn
Copy link

heartnn commented Feb 12, 2020

undefined method `chomp' for nil:NilClass (NoMethodError)

@corona6
Copy link
Author

corona6 commented Mar 18, 2020

@heartnn I added the usage of this. It can use for local file synchronisation. This script needs local file directory path at the argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment