Easy way to archive old messages by year?

122 views
Skip to first unread message

John Kitchin

unread,
Aug 31, 2021, 6:01:30 PM8/31/21
to mu-discuss
I have many years of emails in my mu4e index, and I am interested in moving them to their own folders, e.g. 2017, 2018, etc... 

I know I can do a search in mu4e like  date:20170101..20171231

but it seems to only show 500 results, and it is pretty slow. Is there a command line way to do this with mu?

Joost Kremers

unread,
Aug 31, 2021, 6:15:32 PM8/31/21
to mu-di...@googlegroups.com
I have a bash one-liner that deletes mail that's been sitting in my trash folder
for at least a month:

/usr/local/bin/mu find 'm:/Trash AND d:..1m' --fields l | xargs rm

The `--field l` is crucial here: it outputs the full file path of the messages.
(Piping them to xargs without any concern for spaces in the file paths is not
really a good idea, but it works for me...)


--
Joost Kremers
Life has its moments

Oscar Carlsson

unread,
Sep 1, 2021, 8:40:47 AM9/1/21
to mu-di...@googlegroups.com, John Kitchin
Hi,

I have a snippet for this:

(defun custom/mu4e-set-archive-folder ()
"Have my archive divided into yearly subfolders, please."
(let ((current-year (format-time-string "%Y" (current-time))))
(setq mu4e-refile-folder (concat "/Archive/" current-year))))
(add-hook 'mu4e-headers-mode-hook #'custom/mu4e-set-archive-folder)

I then use it to refile emails when going through my mail.


Oscar

Tassilo Horn

unread,
Sep 1, 2021, 8:53:50 AM9/1/21
to mu-di...@googlegroups.com, John Kitchin
"'Oscar Carlsson' via mu-discuss" <mu-di...@googlegroups.com> writes:

Hi Oscar,

> I have a snippet for this:
>
> (defun custom/mu4e-set-archive-folder ()
> "Have my archive divided into yearly subfolders, please."
> (let ((current-year (format-time-string "%Y" (current-time))))
> (setq mu4e-refile-folder (concat "/Archive/" current-year))))
> (add-hook 'mu4e-headers-mode-hook #'custom/mu4e-set-archive-folder)
>
> I then use it to refile emails when going through my mail.

But then /Archive/2019/ contains the messages you have *refiled* in
2019, not the refiled messages that have been sent in 2019. That is, if
you now refile a message from 2019, it would go into the 2021 folder. I
guess the OP would like to have it refiled to /Archive/2019.

Bye,
Tassilo

Oscar Carlsson

unread,
Sep 2, 2021, 1:53:42 AM9/2/21
to mu-di...@googlegroups.com, John Kitchin, Tassilo Horn
Hi,

Yes, that's very true. I initially tried looking at the timestamp
of
the email to be refiled but settled with this (I might have spent
~30
seconds on this solution).

But I guess it should be fairly easy to look at the email metadata
and
parse it's date. Just a SMOP. :-)


Oscar

Tassilo Horn

unread,
Sep 2, 2021, 9:09:57 AM9/2/21
to mu-di...@googlegroups.com, John Kitchin
"'Oscar Carlsson' via mu-discuss" <mu-di...@googlegroups.com> writes:

>> But then /Archive/2019/ contains the messages you have *refiled* in
>> 2019, not the refiled messages that have been sent in 2019. That is, if
>> you now refile a message from 2019, it would go into the 2021 folder. I
>> guess the OP would like to have it refiled to /Archive/2019.
>
> Yes, that's very true. I initially tried looking at the timestamp of
> the email to be refiled but settled with this (I might have spent ~30
> seconds on this solution).
>
> But I guess it should be fairly easy to look at the email metadata and
> parse it's date. Just a SMOP. :-)

I'm not so sure. What if you select multiple mails from different years
for refiling and then apply marks? At which point can you access each
message individually?

Oh, I think, I found the solution: `mu4e-mark-execute-pre-hook'. So
you'd set `mu4e-refile-folder' in there based on the Date of the
message.

Bye,
Tassilo

Joost Kremers

unread,
Sep 2, 2021, 3:11:21 PM9/2/21
to mu-di...@googlegroups.com, John Kitchin, Tassilo Horn

On Thu, Sep 02 2021, Tassilo Horn wrote:
> I'm not so sure. What if you select multiple mails from different years
> for refiling and then apply marks? At which point can you access each
> message individually?
>
> Oh, I think, I found the solution: `mu4e-mark-execute-pre-hook'. So
> you'd set `mu4e-refile-folder' in there based on the Date of the
> message.

The value of `mu4e-refile-folder` can also be a function, so (if I understand
you correctly), you don't need the hook.

Tassilo Horn

unread,
Sep 2, 2021, 4:21:58 PM9/2/21
to mu-di...@googlegroups.com, John Kitchin, Joost Kremers
Joost Kremers <joostk...@fastmail.fm> writes:

>> I'm not so sure. What if you select multiple mails from different
>> years for refiling and then apply marks? At which point can you
>> access each message individually?
>>
>> Oh, I think, I found the solution: `mu4e-mark-execute-pre-hook'. So
>> you'd set `mu4e-refile-folder' in there based on the Date of the
>> message.
>
> The value of `mu4e-refile-folder` can also be a function, so (if I
> understand you correctly), you don't need the hook.

Gosh, sometimes you don't see the forest for the trees. :-)

Bye,
Tassilo

Norm Tovey-Walsh

unread,
Sep 3, 2021, 7:52:26 AM9/3/21
to mu-di...@googlegroups.com, John Kitchin, Joost Kremers, Tassilo Horn
>> The value of `mu4e-refile-folder` can also be a function, so (if I
>> understand you correctly), you don't need the hook.
>
> Gosh, sometimes you don't see the forest for the trees. :-)

I’ve been meaning to reply on this thread. Apologies if I’m coming too
late to the party. My approach is to bind a different mu4e-refile-folder
(as a function) in each context. One of the providers I use is
fastmail.com, and for that context I have the refile folder set to:

(defun fastmail-mu4e-refile-folder (msg)
"Function for choosing the refile folder for my personal email.
MSG is a message p-list from mu4e."
(let ((date (mu4e-message-field msg :date)))
(cond
(date
(format "/Fastmail/archive-%s" (format-time-string "%Y-%m" date)))
(t
"/Fastmail/Archive"))))

My workflow is to refile messages out of my INBOX as I process them and
these functions move them into folders archived by year and month. Every
now and then I archive off the folders more than a couple years old.

FWIW.

Be seeing you,
norm

--
Norman Tovey-Walsh <n...@nwalsh.com>
https://nwalsh.com/

> Quotation, n: The act of repeating erroneously the words of
> another.--Ambrose Bierce
signature.asc
Reply all
Reply to author
Forward
0 new messages