Function: org-persist-gc

org-persist-gc is a byte-compiled function defined in org-persist.el.gz.

Signature

(org-persist-gc)

Documentation

Remove expired or unregistered containers and orphaned files.

Also, remove containers associated with non-existing files.

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-persist.el.gz
(defun org-persist-gc ()
  "Remove expired or unregistered containers and orphaned files.
Also, remove containers associated with non-existing files."
  (if org-persist--index
      (org-persist--merge-index-with-disk)
    (org-persist--load-index))
  (let (new-index
        (remote-files-num 0)
        (orphan-files
         (when (org-persist--gc-orphan-p) ; also removes current session from lock file.
           (delete (org-file-name-concat org-persist-directory org-persist-index-file)
                   (when (file-exists-p org-persist-directory)
                     (directory-files-recursively org-persist-directory ".+"))))))
    (dolist (collection org-persist--index)
      (let* ((file (plist-get (plist-get collection :associated) :file))
             (web-file (and file (string-match-p "\\`https?://" file)))
             (file-remote (when file (file-remote-p file)))
             (persist-file (when (plist-get collection :persist-file)
                             (org-file-name-concat
                              org-persist-directory
                              (plist-get collection :persist-file))))
             (expired? (org-persist--gc-expired-p
                        (plist-get collection :expiry) collection)))
        (when persist-file
          (setq orphan-files (delete persist-file orphan-files))
          (when (and file (not web-file))
            (when file-remote (cl-incf remote-files-num))
            (unless (if (not file-remote)
                        (file-exists-p file)
                      (pcase org-persist-remote-files
                        ('t t)
                        ('check-existence
                         (file-exists-p file))
                        ((pred numberp)
                         (< org-persist-remote-files remote-files-num))
                        (_ nil)))
              (setq expired? t)))
          (if expired?
              (org-persist--gc-persist-file persist-file)
            (push collection new-index)
            (dolist (container (plist-get collection :container))
              (dolist (associated-file
                       (org-persist-associated-files:generic
                        container collection))
                (setq orphan-files (delete associated-file orphan-files))))))))
    (mapc #'org-persist--gc-persist-file orphan-files)
    (setq org-persist--index (nreverse new-index))))