-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfriendica-delete-old-posts.sh
More file actions
191 lines (183 loc) · 11.4 KB
/
friendica-delete-old-posts.sh
File metadata and controls
191 lines (183 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
#Enables optimizations to make the deletion take less time. Set to 0 to delete more items. Set to 2 to also delete items from your followed accounts (this will break reposts and mentions)
intense_optimizations=${1:-"1"}
#Determines the interval of deletion. Defaults to 90 days, same as Friendica.
i=${2:-"90"}
interval="${i} DAY"
dbengine="mariadb"
db="friendica"
#First, search all reserved matches where:
#- the post is from a followed contact,
#- the post if from a user of this instance, whether due to the "user" or "contact" IDs,
#- or the post is from a member of a group.
sudo "${dbengine}" "${db}" -vvve "\
drop table if exists tmp_post_matches; \
create table tmp_post_matches (\
select distinct p.\`uri-id\` from \`post\` p \
where p.\`author-id\` in (select \`cid\` from \`user-contact\`) \
or p.\`causer-id\` in (select \`cid\` from \`user-contact\`) \
or p.\`owner-id\` in (select \`cid\` from \`user-contact\`) \
or p.\`author-id\` in (select \`uid\` from \`user\` where \`uid\` != \"0\") \
or p.\`causer-id\` in (select \`uid\` from \`user\` where \`uid\` != \"0\") \
or p.\`owner-id\` in (select \`uid\` from \`user\` where \`uid\` != \"0\") \
or p.\`author-id\` in (select \`id\` from \`contact\` where \`gsid\` = \"1\") \
or p.\`causer-id\` in (select \`id\` from \`contact\` where \`gsid\` = \"1\") \
or p.\`owner-id\` in (select \`id\` from \`contact\` where \`gsid\` = \"1\") \
or p.\`author-id\` in (select \`contact-id\` from \`group_member\`) \
or p.\`causer-id\` in (select \`contact-id\` from \`group_member\`) \
or p.\`owner-id\` in (select \`contact-id\` from \`group_member\`) \
);" #&> /dev/null
#Show only in case we're not optimizing for speed.
if [[ ${intense_optimizations} -eq 0 ]]; then
#For safety, find all the posts that would somehow match both criteria and thus be wrongly deleted. We want this to return no results.
#Then, show how many posts would be deleted this way, sorted by date.
sudo "${dbengine}" "${db}" -vvve "\
create temporary table tmp_post (\
select distinct p.\`uri-id\` from \`post\` p \
where p.\`uri-id\` not in (select \`uri-id\` from \`tmp_post_matches\`) \
and p.\`created\` < CURDATE() - INTERVAL ${interval} \
); \
select t.\`id\`, t.\`name\`, t.\`addr\`, p.\`created\`, c.\`body\` from \`tmp_post\` tmp \
inner join \`post\` p on p.\`uri-id\` = tmp.\`uri-id\` \
inner join \`post-content\` c on p.\`uri-id\` = c.\`uri-id\` \
inner join \`contact\` t on t.\`id\` = p.\`author-id\` \
where p.\`author-id\` in (select \`uid\` from \`user\` where \`uid\` != \"0\") \
or p.\`causer-id\` in (select \`uid\` from \`user\` where \`uid\` != \"0\") \
or p.\`owner-id\` in (select \`uid\` from \`user\` where \`uid\` != \"0\") \
or p.\`author-id\` in (select \`id\` from \`contact\` where \`gsid\` = \"1\") \
or p.\`causer-id\` in (select \`id\` from \`contact\` where \`gsid\` = \"1\") \
or p.\`owner-id\` in (select \`id\` from \`contact\` where \`gsid\` = \"1\") \
order by p.\`created\` desc limit 100; \
select count(p.\`uri-id\`), p.\`created\` from \`post\` p \
inner join tmp_post tmp on p.\`uri-id\` = tmp.\`uri-id\` \
group by date_format(p.\`created\`, \"%y%m\") \
order by \`created\` desc;" #&> /dev/null
fi
if [[ ${intense_optimizations} -lt 2 ]]; then
#Filter all the non-matching items (which we do want to delete) that are older than the given interval.
#Finally, proceed with the deletion.
sudo "${dbengine}" "${db}" -vvve "\
create temporary table tmp_post (\
select distinct p.\`uri-id\` from \`post\` p \
where p.\`uri-id\` not in (select \`uri-id\` from \`tmp_post_matches\`) \
and p.\`created\` < CURDATE() - INTERVAL ${interval} \
); \
delete from \`post-thread\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-thread-user\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-user\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-tag\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-content\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-media\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-counts\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-category\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-history\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
" #&> /dev/null
fi
#Show only in case we're not optimizing for speed.
if [[ ${intense_optimizations} -eq 0 ]]; then
#Find any posts created before the first user of the instance registered.
#For safety, search all the reserved matches, then find all the posts that would somehow match the criteria and thus be wrongly deleted. We want this to return no results.
#Then, show how many posts would be deleted this way, sorted by date.
#Finally, proceed with the deletion.
sudo "${dbengine}" "${db}" -vvve "\
create temporary table tmp_post (\
select distinct p.\`uri-id\` from \`post\` p \
where p.\`created\` < (
select \`register_date\` from \`user\` \
order by \`register_date\` asc limit 1 \
) \
); \
select t.\`id\`, t.\`name\`, t.\`addr\`, p.\`created\`, c.\`body\` from \`tmp_post\` tmp \
inner join \`post\` p on p.\`uri-id\` = tmp.\`uri-id\` \
inner join \`post-content\` c on p.\`uri-id\` = c.\`uri-id\` \
inner join \`contact\` t on t.\`id\` = p.\`author-id\` \
where p.\`author-id\` in (select \`uid\` from \`user\` where \`uid\` != \"0\") \
or p.\`causer-id\` in (select \`uid\` from \`user\` where \`uid\` != \"0\") \
or p.\`owner-id\` in (select \`uid\` from \`user\` where \`uid\` != \"0\") \
or p.\`author-id\` in (select \`id\` from \`contact\` where \`gsid\` = \"1\") \
or p.\`causer-id\` in (select \`id\` from \`contact\` where \`gsid\` = \"1\") \
or p.\`owner-id\` in (select \`id\` from \`contact\` where \`gsid\` = \"1\") \
order by p.\`created\` desc limit 100; \
select count(p.\`uri-id\`), p.\`created\` from \`post\` p \
inner join tmp_post tmp on p.\`uri-id\` = tmp.\`uri-id\` \
group by date_format(p.\`created\`, \"%y%m\") \
order by \`created\` desc; \
delete from \`post-thread\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-thread-user\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-user\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-tag\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-content\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-media\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-counts\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-category\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-history\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
" #&> /dev/null
fi
#Execute only for the most intensive deletion.
if [[ ${intense_optimizations} -eq 2 ]]; then
#First, search all reserved matches where the post if from a user of this instance, whether due to the "user" or "contact" IDs.
#Then, filter all the non-matching items (which we do want to delete) that are older than the given interval.
#For safety, find all the posts that would somehow match the standard criteria and thus be wrongly deleted. We want this to return no results.
#Then, show how many posts would be deleted this way, sorted by date.
#Finally, proceed with the deletion.
sudo "${dbengine}" "${db}" -vvve "\
create temporary table tmp_post_matches_unrestricted (\
select distinct p.\`uri-id\` from \`post\` p \
where p.\`author-id\` in (select \`uid\` from \`user\` where \`uid\` != \"0\") \
or p.\`causer-id\` in (select \`uid\` from \`user\` where \`uid\` != \"0\") \
or p.\`owner-id\` in (select \`uid\` from \`user\` where \`uid\` != \"0\") \
or p.\`author-id\` in (select \`id\` from \`contact\` where \`gsid\` = \"1\") \
or p.\`causer-id\` in (select \`id\` from \`contact\` where \`gsid\` = \"1\") \
or p.\`owner-id\` in (select \`id\` from \`contact\` where \`gsid\` = \"1\") \
); \
create temporary table tmp_post (\
select distinct p.\`uri-id\` from \`post\` p \
where p.\`uri-id\` not in (select \`uri-id\` from \`tmp_post_matches_unrestricted\`) \
and p.\`created\` < CURDATE() - INTERVAL ${interval} \
); \
select t.\`id\`, t.\`name\`, t.\`addr\`, p.\`created\`, c.\`body\` from \`tmp_post\` tmp \
inner join \`post\` p on p.\`uri-id\` = tmp.\`uri-id\` \
inner join \`post-content\` c on p.\`uri-id\` = c.\`uri-id\` \
inner join \`contact\` t on t.\`id\` = p.\`author-id\` \
where p.\`author-id\` in (select \`uid\` from \`user\` where \`uid\` != \"0\") \
or p.\`causer-id\` in (select \`uid\` from \`user\` where \`uid\` != \"0\") \
or p.\`owner-id\` in (select \`uid\` from \`user\` where \`uid\` != \"0\") \
or p.\`author-id\` in (select \`id\` from \`contact\` where \`gsid\` = \"1\") \
or p.\`causer-id\` in (select \`id\` from \`contact\` where \`gsid\` = \"1\") \
or p.\`owner-id\` in (select \`id\` from \`contact\` where \`gsid\` = \"1\") \
order by p.\`created\` desc limit 100; \
select count(p.\`uri-id\`), p.\`created\` from \`post\` p \
inner join tmp_post tmp on p.\`uri-id\` = tmp.\`uri-id\` \
group by date_format(p.\`created\`, \"%y%m\") \
order by \`created\` desc; \
delete from \`post-thread\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-thread-user\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-user\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-tag\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-content\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-media\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-counts\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-category\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post-history\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
delete from \`post\` where \`uri-id\` in (select \`uri-id\` from \`tmp_post\`); \
" #&> /dev/null
fi
if [[ ${intense_optimizations} -ne 1 ]]; then
#Show all the leftover posts, sorted by date.
sudo "${dbengine}" "${db}" -e 'select count(*), `created` from `post` group by date_format(`created`, "%y%m") order by `created` desc' #&> /dev/null
fi
#Fix the auto-increment for the affected tables. Clear temporary tables.
sudo "${dbengine}" "${db}" -v -e "\
alter table \`post-thread\` auto_increment = 1; \
alter table \`post-thread-user\` auto_increment = 1; \
alter table \`post-user\` auto_increment = 1; \
alter table \`post-tag\` auto_increment = 1; \
alter table \`post-content\` auto_increment = 1; \
alter table \`post-media\` auto_increment = 1; \
alter table \`post-counts\` auto_increment = 1; \
alter table \`post-category\` auto_increment = 1; \
alter table \`post-history\` auto_increment = 1; \
alter table \`post\` auto_increment = 1; \
drop table if exists tmp_post_matches;" #&> /dev/null