How can I remove a directory and all its contents?

From @jamiewilson on Mon Mar 20 2017 20:53:50 GMT+0000 (UTC)

I have this object, a directory with the following contents:

$ ipfs file ls fZXmMYYCQYeUUadmvx23LXR3CQRGoK881mJoKAPT8fZGR6
css
img
index.html

But when I go to remove this folder with the ipfs files rm command, I get the following message:

$ ipfs files rm -r fZXmMYYCQYeUUadmvx23LXR3CQRGoK881mJoKAPT8fZGR6
Error: Paths must start with a leading slash.

$ ipfs files rm -r /fZXmMYYCQYeUUadmvx23LXR3CQRGoK881mJoKAPT8fZGR6
Error: merkledag: not found

Does anyone know what I’m doing wrong here? Thanks for the help.


Copied from original issue: https://github.com/ipfs/faq/issues/241

1 Like

I am having this same problem, is it answered somewhere?

Hey guys!

I think there’s some misconception:

A CID is a pin. You can unpin or pin a CID.

If you unpin it, the garbage collection will remove it after a while.

If the CID contains a folder, the ‘ipfs files ls’ can also list it’s content.

To use ipfs files rm the CID must be locally stored as a file/folder, not just pinned.

You can pin a folder locally like this:

‘ipfs files cp /ipfs/$CID /time_travel_stuff’

Locally stored files and folders can be removed from the local directory again:

‘ipfs files rm /time_travel_stuff’

A CID local file/folder must be addressed with their local path, not the CID, because it’s basically just a ‘link’ to a CID you’re removing.

You can for example link one CID at two places, without storing the data twice:

‘ipfs files cp /ipfs/$CID /home-movies/2019-04/swimming-pool.mp4’

And add it at a different location:

‘ipfs files cp /ipfs/$same_CID /shares/mike/home-movie-swimming-pool.mp4’

You can now share the CID of /shares/mike with Mike to allow him to find the video.

If you want to remove the file again from the folder, you can just run

‘ipfs files rm /shares/mike/home-movie-swimming-pool.mp4’

Add other stuff and share the new CID of the folder again with Mike.

Note: All changes to a folder will change it’s CID, so Mike can still access the old version with the old CID and access also the movie after you removed it from your folder.

Never expect that you can remove stuff from IPFS after you added it, it’s not meant to be used that way.

I was also asking on IRC/Matrix about this and the answer I was looking for is:

ipfs pin rm --recursive=true hashhere
ipfs repo gc

Thank you

Hey Mikaela,

the ‘ipfs repo gc’ operation is very IO intensive. You really shouldn’t use it, if you don’t have to. It will also remove everything else from the cache which isn’t pinned. So if you use the browser plugin for example, all locally cached files for the ipfs websites are removed as well and need to be downloaded again.

The cache of IPFS will be garbage collected automatically if necessary.

Also note that pin rm is always recursive, you don’t need the flag.

Hi, I opened a new thread that I hope makes my problem and what I am looking to do more clear: How can I be certain that my IPFS node is not hosting content X short of `rm -rf ~/.ipfs`?

I think you will find it that it was an necessary option to use.

I am confused as on IRC/Matrix I was told the opposite and to look at ipfs pin rm --help which says ipfs pin rm [--recursive=false] [--] <ipfs-path>...which I take to mean that it defaults to recursive=false and thus is not always recursive when not otherwise specified.

The --help text on pin rm states:

DESCRIPTION

  Removes the pin from the given object allowing it to be garbage
  collected if needed. (By default, recursively. Use -r=false for direct pins.)

The text in the brackets in the Synopsis only highlights optional parameters, not which parameters are set by default to which value.

1 Like

Hah, I guess I and the people on IRC/Matrix missed that point, pointing me only to what I quoted above. Thank you :slight_smile: