Tuesday, January 17, 2012

How to delete packages created before certain date from package manager using curl in CQ / WEM

Use case: You upload CQ packages a lot and end up having many packages in package manager. This cause package manager to load slow, also cause un necessary space consumption.

Solution: You can use following unix script to delete packages created before (Or after) certain date.

Please change variable based on your need.

I have commented curl command for now, First see if right package is getting deleted then uncomment curl command to delete.

#!/bin/bash

# Author: upadhyay.yogesh@gmail.com

# The host and port of the source server

SOURCE="localhost:4504"

# The user credentials on the source server (username:password)

SOURCE_CRED="admin:admin"

#This is your package location in CRX. Change this package location based on your need

PACKAGE_LOCATION="/etc/packages/my_packages"

# This is date after which you want to delete all package <YYYY>-<MM>-<DD>

DATE="2011-12-11"

#############################################################################

#Query to get all files uploaded after certain date

ALL_PATHS=`curl -s -u $SOURCE_CRED "$SOURCE/bin/querybuilder.json?path=$PACKAGE_LOCATION&type=nt:file&p.limit=-1&daterange.property=jcr:created&daterange.upperBound=2011-11-11&daterange.upperOperation=

echo "$ALL_PATHS"

for SINGLE_PATH in $ALL_PATHS

do

# I have commented this line. Please see what is getting deleted before deleting it

# curl -u $SOURCE_CRED -X POST http://$SOURCE/crx/packmgr/service/.json$SINGLE_PATH?cmd=delete

done


You can also do if else within script to compare package name


if [[ $string =~ .*My.* ]]

then

DELETE PACKAGE

fi


Please check http://dev.day.com/docs/en/crx/current/how_to/package_manager.html#Managing Packages on the Command Line for different curl options

IMPORTANT NOTE: Please test this script before use. I have not tested this script yet :)

19 comments:

  1. Using curl I have to build, download a package from one instance and upload, install in another instance. I have created a package and filter definitions and built the package from command line. All these steps should happen in windows envt in a .bat file.
    But I am having issues with simple download of package when I run the curl command. It returns gibberish... I would like to download the file to the current folder and upload to the new instance. Could you let me know the reason for the issue and any better approach.

    -Gunesh

    ReplyDelete
  2. Gunesh,

    What curl command you are using to download ?

    Yogesh

    ReplyDelete
  3. Yogesh,
    I am using this command in a batch file
    curl -u admin:admin http://localhost:4502/etc/packages//
    All other curl commands are working except this one on 5.4.

    ReplyDelete
  4. Correction:
    curl -u admin:admin http://localhost:4502/etc/packages/groupname/packagename.zip

    ReplyDelete
  5. Gunesh,

    Use

    curl -C - -O -u admin:admin http://localhost:4502/etc/packages/groupname/packagename.zip

    Let me know if that works for you.

    Yogesh

    ReplyDelete
  6. Yogesh,
    This definitely works. Thanks. Also if I download a package to current folder and download it again with already existing package to the same folder, how to overwrite the existing package. I achieved it by deleting from current folder before downloading it again, but it would be great if I can overwrite the same package. I am using the curl for download from batch script.

    Gunesh

    ReplyDelete
  7. Gunesh,

    You can use force upload option. I think it is force=true as form data in post.

    Yogesh

    ReplyDelete
  8. Hi Yogesh,

    I need to build a package using curl command. I am using the below command:
    curl -X POST http://localhost:5412/crx/packmgr/service/.json/etc/packages/my_packages/Url-shorten.zip?cmd=build

    The result is : {"success":false,"msg":"no package"}

    Any thoughts?

    I am using CQ 5.4

    Thanks in advance

    Regards,
    Pradeep

    ReplyDelete
  9. I think you are missing -u : ... If package exist, Above command with -u should work. I just tested and it works for me.

    curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/config.zip?cmd=build
    {"success":true,"msg":"Package built"}

    ReplyDelete
  10. I'm able to create a package using the following command -
    curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages?cmd=create -d packageName=testpackage -d groupName=day {"success":true,"msg":"Package created","path":"/etc/packages/day/testpackage.zip"}

    But have no idea on how to set multiple filter(paths) conditions? Please can you let me know how to do this.

    ReplyDelete
    Replies
    1. Hari,

      You can also use curl script to delete all packages as mention here http://www.wemblog.com/2013/05/how-to-perform-system-clean-up-in-adobe.html

      Yogesh

      Delete
  11. hi
    can you create a package with some filters using curl ??

    ReplyDelete
    Replies
    1. Please check http://www.wemblog.com/2012/04/how-to-change-package-install-behavior.html you can dynamically change filter to create package.

      Delete
  12. Hi Yogesh

    Would this method work if you wanted to purge backup packages from .snapshot?

    ReplyDelete
    Replies
    1. Yes. Should work. Else you can always get to .snapshot folder and delete them.

      Yogesh

      Delete
  13. This comment has been removed by the author.

    ReplyDelete
  14. I need to upload a package from local to server using curl command. I am using the below command:
    curl -u -F package=C:/Users/Downloads/package.zip -X POST http:///crx/packmgr/service/.json/?cmd=upload

    The result is : {"success":false,"msg":"package filter parameter missing"}

    or else

    I need to download package from one instance and also i need to upload same package into same instance.can you provide the curl command for this??
    please help me out !!

    ReplyDelete
  15. Apologies..

    The command i am using..
    curl -u : -F package=c:/user/downloads/package.zip http://localhost:4502/crx/packmgr/service/.json/?cmd=upload


    The result is : {"success":false,"msg":"package filter parameter missing"}

    ReplyDelete
  16. Hi Team,

    I want to delete packages between certain time period like 2015-06-11 to 2015-06-18. I tried all the possibilities mentioned here.

    And one more requirement is like "I want to delete all the old packages if any one of the existing packages contains the same content which I am uploading now."

    Could u please help me out in writing the curl command.

    With Regards,
    Sravani.

    ReplyDelete