General Question

bomyne's avatar

How do you delete an oversized directory in linux?

Asked by bomyne (639points) June 19th, 2011

On my Linux Server (CentOS 5 or something), I have a mail account that’s way over sized. It’s 4.9GB worth of mail. Attempting to access that account via pop3 causes timeouts, attempting to delete it via cPanel and WHM causes timeouts. And if I try to delete it via FTP or even SSH, it causes the server load to skyrocket.

Is there a way I can delete this mail account, or at least empty it, without overloading the server?

I think the account in question is in Maildir format. and the mailserver itself is “Courier”.

Observing members: 0 Composing members: 0

17 Answers

Neurotic_David's avatar

$ rm -rf <name of maildir>

bomyne's avatar

I tried that. All it does is cause the server to overload and stop responding.

jaytkay's avatar

Is it one giant file or many files?

If many:
In Windows, I have solved problems like this by writing a batch program to delete a few files at a time, 10 or 100 or whatever number works

Neurotic_David's avatar

cd into the directory and rm the files one by one?

bomyne's avatar

I’m guessing it’s many files, but it’s impossible to do a ls as well as that also causes the system to overload and stop responding.

robmandu's avatar

try this:

find <path to maildir> -type f -mdate -1 -exec rm -f {} \;

The idea is to use the find command to build individual rm commands for each file one at a time. The mdate option is for files less than 1 day old, which limits the number of files being acted on.

If this works, then change mdate to -2 and run, then -3, etc.

bomyne's avatar

I’ll try that, Thanks. If I want it to be verbose, do I add the v to the rm -f?

or is that a bad idea?

bomyne's avatar

It gives me an error when attempting that

find: invalid predicate `-mdate’

root@server [/home/<user>]# find—version
GNU find version 4.2.27
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX

koanhead's avatar

If attempting to ls or rm files in a directory makes the server unresponsive then there’s something else wrong.

Unless you’ve radically underprovisioned your server then there’s no reason why it should be so overloaded. It sounds like you are using a small VPS- there are lots of possible reasons why these can have problems.
If I’m right in guessing that it’s a VPS then I recommend that you contact your provider’s technical support and ask them to look into it. You might be able to get them to delete files in that directory (do not delete the directory itself, unless you are shutting down the MDA) from the console. However, this will not solve your problem.

Something is loading your server to the point where it’s having trouble performing simple file operations. It’s possible that the MDA is trying to write to $MAILDIR at the same time that you are trying to delete from it, which can cause trouble. How critical is the mail service? Can you shut it down temporarily while you clean house?
Also, how do you know that the server load increases when you are performing the cited file operations? Are you running ‘top’ while you do them? When top is running, do you see anything suspicious like: a large number of instances of a particular process running; processes owned by unfamiliar usernames or users who should not be logged-in; processes using an unusual amount of RAM or I/O?
Have you tried using ‘netstat -tunapc’ to see if there are unfamiliar processes accessing the network on unfamiliar ports?
Does the server provide services other than email?

bomyne's avatar

The server isn’t VPS. It’s a dedicated machine, and it provides services such as FTP, www, etc. The usuals.

I’m able to monitor the server load though cPanel. It has a page relating to load.

The mail service isn’t critical, so I’ll try shutting it down and see if that helps.

robmandu's avatar

whoops, sorry… it’s not mdate… I should’ve wrote mtime:

find <path to maildir> -type f -mtime -1 -exec rm -f {} \;

Try man find for more information.

And yes, the -v switch on the rm command will enable verbose output.

bomyne's avatar

I’ve shut down the mail service, and ran the updated command @robmandu gave me, with the v on the rm command… nothing appears to be happening

According to the process list, it IS running.

bomyne's avatar

According to the process manager, that command is taking up 1.4%* of CPU and 4.2% of RAM.

Is that to be expected?

For reference:

Total processors: 1

Processor #1

Vendor
GenuineIntel

Name
Intel® Pentium® 4 CPU 2.80GHz

Speed
2813.575 MHz

Cache
512 KB

Memory: 2063824k/2088896k available (2161k kernel code, 23804k reserved, 900k data, 228k init, 1171392k highmem)

EDIT: * = Gone up to 2.3%

bomyne's avatar

For some reason, Fluther won’t let me edit that post again >.<

Looks like the command completed but it doesn’t appear to have made a difference. I’ve run ls in that directory and it’s sitting there doing nothing.

The process for ls is using 7.9% of the CPU and rising. and well over 15% of RAM.

robmandu's avatar

It only deleted files less than 24 hours old (1 day, hence -1).

Run it again for 2, 3 or 356 days. The main point of the mtime parameter was to give you control over how many files are being deleted at once, so as not to crash the box like you were seeing.

Or, you can remove the mtime parameter altogether.

bomyne's avatar

I set it to 365 and it’s working. I’m watching it delete files. Thank you so much for your help!

Response moderated (Spam)

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther