本文目录导读:

- WordPress 站点清理未使用的评论通知
- 清理第三方评论系统(如 Disqus)中的未使用通知
- 清理 GitHub Issues/PR 中的未使用通知
- 通用:清理系统级通知缓存(如 Laravel 通知表)
- 自动化清理的关键步骤
针对自动化清理未使用的评论通知,通常需要根据具体的平台(如 WordPress、Disqus、GitHub Issues 等)或系统类型(如 CMS、论坛、文档系统)编写脚本,以下是几种常见场景的自动化清理策略和脚本示例:
WordPress 站点清理未使用的评论通知
WordPress 会存储大量已删除或未关联的评论元数据、通知缓存,可通过 WP-CLI 或直接 SQL 清理:
脚本示例(Bash + WP-CLI):
#!/bin/bash # 清理所有未关联的评论通知(如垃圾评论、待审核评论) wp comment delete $(wp comment list --status=spam --format=ids) --force wp comment delete $(wp comment list --status=trash --format=ids) --force # 清除评论缓存(如果使用 Redis 或对象缓存) wp cache flush # 可选:清理评论元表中不再使用的数据 wp db query "DELETE FROM wp_commentmeta WHERE comment_id NOT IN (SELECT comment_id FROM wp_comments);"
注意事项:
- 先备份数据库再运行
- 可配合
cron定时执行(如每月一次)
清理第三方评论系统(如 Disqus)中的未使用通知
Disqus 不直接提供 API 批量删除未使用的通知,但可自动化标记/隐藏已关闭线程的评论:
脚本示例(Python + Disqus API):
import requests
import time
API_KEY = "你的Disqus公钥"
ACCESS_TOKEN = "你的OAuth令牌"
FORUM = "你的论坛名称"
# 获取所有已关闭的线程
threads_url = f"https://disqus.com/api/3.0/threads/list.json?forum={FORUM}&api_key={API_KEY}&access_token={ACCESS_TOKEN}&filter=closed"
response = requests.get(threads_url).json()
closed_threads = [thread['id'] for thread in response['response']]
# 清理这些线程下的未读通知(标记为已读)
for thread_id in closed_threads:
posts_url = f"https://disqus.com/api/3.0/posts/list.json?thread={thread_id}&api_key={API_KEY}&access_token={ACCESS_TOKEN}&limit=100"
posts = requests.get(posts_url).json()['response']
for post in posts:
# 标记所有旧帖子为已读(实际通知由 Disqus 管理,清理需手动忽略)
if not post.get('isRead'):
# Disqus 无直接标记已读的 API,需在用户界面操作
pass
time.sleep(0.5) # 避免 API 限流
替代方案:
运行 Disqus 内置的自动清理功能(如删除 90 天前的无效通知),通常设置在论坛后台 Admin > Community > Moderation。
清理 GitHub Issues/PR 中的未使用通知
GitHub API 不提供直接删除通知,但可关闭/合并已停滞的 PR 或 Issues,从而清除关联通知:
脚本示例(Python + PyGithub):
from github import Github
import datetime
g = Github("your_github_token")
repo = g.get_repo("owner/repo")
# 清理 90 天未更新的 Issues(状态为 open,无标签)
deadline = datetime.datetime.now() - datetime.timedelta(days=90)
for issue in repo.get_issues(state='open'):
if issue.pull_request is None: # 只处理 Issues
if issue.updated_at < deadline and len(issue.labels) == 0:
issue.edit(state='closed', state_reason='not_planned')
print(f"Closed unused issue: {issue.number}")
注意事项:
- 需要 GitHub 个人访问令牌
- 可设置 API 调用间隔避免限流
通用:清理系统级通知缓存(如 Laravel 通知表)
对于框架生成的未使用通知(如用户已删除但通知仍存于数据库),可运行:
SQL 脚本示例(MySQL):
-- 清理 30 天前的未读通知 DELETE FROM notifications WHERE read_at IS NULL AND created_at < DATE_SUB(NOW(), INTERVAL 30 DAY); -- 清理已删除用户的通知 DELETE n FROM notifications n LEFT JOIN users u ON n.notifiable_id = u.id WHERE u.id IS NULL;
配合 Cron 定期执行:
0 3 * * 0 mysql -u root -p'密码' -e "DELETE FROM notifications WHERE ..." your_database
自动化清理的关键步骤
-
识别未使用条件:
- 按时间(30/90 天未更新)
- 按状态(垃圾/已删除/孤儿数据)
- 按关联对象(用户/线程已不存在)
-
测试模式:
先运行--dry-run或输出计划删除的条目数,验证无误后再执行实际删除。 -
备份与回滚:
运行前导出相关数据:mysqldump -u root your_db notifications > backup.sql -
可维护性:
将脚本加入定时任务(Linuxcron/ WindowsTask Scheduler),并记录日志:0 2 * * 1 /path/to/cleanup.sh >> /var/log/notification_cleanup.log 2>&1
- WordPress:使用 WP-CLI 或 SQL 直接清理已废弃的评论
- Disqus:标记已读或定期删除快照,需结合平台默认清理规则
- GitHub:关闭过时 Issues 间接减少通知
- 自定义系统:编写 SQL 或使用 ORM 清理孤儿记录
如果需要更具体的脚本(清理特定 CMS 如 Discourse 的通知),请补充系统名称或提供更多细节。