Bulk remove databags from chef server?

729 Views Asked by At

I'm using a databag for package versions, e.g.

databags/
  versions/
    0.1.1
    0.1.2
    0.1.3
    0.2.1
    0.2.2
    ...

When the version becomes obsolete, I want to purge the old data bag items. Is there a way to bulk delete the data bag items using, for example, a regex? Something like:

knife data bag delete versions 0.1.*

Currently, I have to query the server and iterate over the results, which entails multiple knife calls.

2

There are 2 best solutions below

1
On

Perhaps something like this:

knife exec -E 'api.get("/data/versions").select{|v| v.start_with?('0.1')}.each{|v| api.delete("/data/versions/#{v}")'

0
On

I would use the repo based databags for this.

If you do a knife data bag from file 'folder' it will overwrite the databag with the items (jsno files) in this folder. Documentation here

So having a clean folder with only the wanted json for each item on filesystem and managing the content with file system tools would do and you'll end up with a single command.

If you don't already have your databags on filesystem, you may create a directory per databag and then a knife data bag show BAG ITEM -F json > databag_folder/item.json to create each item (looping over a knife data bag show may help)

There's maybe already a kind of knife plugin for this, but I was not able to find it.

Edit after a quick search: http://docs.chef.io/chef/knife.html#id133 seems this command can take a pattern to delete databags items/cookbooks.