How to Delete All Contents of a Folder with Ruby-Rails

How to delete all contents of a folder with Ruby-Rails?

Ruby has the *nix rm -rf equivalent in the FileUtils module that you can use to delete both files and non-empty folders/directories:

FileUtils.rm_rf('dir/to/remove')

To keep the directory itself and only remove its contents:

FileUtils.rm_rf(Dir.glob('dir/to/remove/*'))

FileUtils.rm_rf(Dir['dir/to/remove/*']) # shorter version of above

How to delete the directory in ruby on rails after its creation without page reload

The stream was not closing after writing files in rubyzip class
I have modified the code in rubyzip class like this

disk_file = File.open(diskFilePath, "rb")
io.get_output_stream(zipFilePath) { |f|
f.puts(disk_file.read())
}
disk_file.close

Empty directory (delete all files)

You're probably getting that error because your current working directory doesn't match dir_path -- File.delete(f) is being given just the filename for a file in dir_path. (I hope you didn't have any important files in the current working directory with same names in the dir_path directory.)

You need to use File.join(dir_path, f) to construct the filename you wish to delete. You also need to figure out how you want to handle directories:

Dir.foreach(dir_path) do |f|
fn = File.join(dir_path, f)
File.delete(fn) if f != '.' && f != '..'
end

Errno::EISDIR: Is a directory - /tmp/testing/blubber
from (irb):10:in `delete'
from (irb):10
from (irb):10:in `foreach'
from (irb):10
from :0

ruby - delete all files with names matching a pattern

Dir::glob supports a single character wildcard (i.e. ?). Based on your example, you could locate the appropriate files in a given directory using ? and then delete them.

Dir.glob('/home/your_username/Documents/page_code?.txt').each { |file| File.delete(file)}

Method to delete all files in Ruby

Following line will help,

FileUtils.rm_rf(Dir['./report/js_errors', './report/screenshots']) 

Ruby deleting directories

Realised my error, some of the files hadn't been closed.
I earlier in my program I was using

File.open(filename).read

which I swapped for a

f = File.open(filename, "r")
while line = f.gets
puts line
end
f.close

And now

FileUtils.rm_rf(dirname)

works flawlessly

Delete a directory from a script

FileUtils.rm_rf doesn't accept wildcards on its own. If you want want keep the cache directory itself, and remove its contents only, use a glob:

FileUtils.rm_rf Dir.glob('absolute_path/tmp/cache/*')

Remove all data from Active Storage?

This question challenged me, so I did some test on my dummy app with local storage.

I have the usual model User which has_one_attached :avatar

On local storage files are saved on /storage folder, under subfolders named randomly with a string of two characters.

Informations related to files are stored in two tables:

  • ActiveStorage::Attachment
  • ActiveStorage::Blob

To completely clean the two tables, I did in rails console:

ActiveStorage::Attachment.all.each { |attachment| attachment.purge }

This command deletes

  • All record in that table: ActiveStorage::Attachment.any? #=> false
  • All the blobs: ActiveStorage::Blob.any? #=> false
  • All the files located under /storage subfolders; of course, subfolders are still there empty.

The ActiveStorage still works poperly.

I expect the same behaviour for remote storage, having the right privileges.

What a safe and easy way to delete a dir in Ruby?

require 'fileutils'

FileUtils.rm_rf(dir)


Related Topics



Leave a reply



Submit