Message Boards

delete liferay user programmatically

AB Bader, modified 8 Years ago.

delete liferay user programmatically

Junior Member Posts: 47 Join Date: 8/5/14 Recent Posts
Hello I'm working on liferay 6.2 CE ga2 and i need to remove user programmatically !
I tested this code but it does not work :


               
        User u=UserLocalServiceUtil.getUser(11612)	;

	UserLocalServiceUtil.updateStatus(u.getUserId() ,WorkflowConstants.STATUS_INACTIVE);  //inactive user
        UserLocalServiceUtil.deleteUser(u.getUserId());   //delete user
	
	


thanks for your help
thumbnail
David H Nebinger, modified 8 Years ago.

RE: delete liferay user programmatically

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
What do you mean, "does not work"?

It throws an exception?

It appears to work but user is still in database?

User shows up in search?
AB Bader, modified 8 Years ago.

RE: delete liferay user programmatically

Junior Member Posts: 47 Join Date: 8/5/14 Recent Posts
It throws this exception :
com.liferay.portal.NoSuchGroupException: No Group exists with the key {companyId=10154, classNameId=10190, classPK=100601}
thumbnail
David H Nebinger, modified 8 Years ago.

RE: delete liferay user programmatically

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
I believe when you create a user it in turn creates a user group with the same name and adds the user to it. Possibly you had deleted the group before or tried the delete of a user and it got through the group deletion but failed further on so now you're in an unknown state.

Normally I never delete users, just mark them as inactive. They cannot log in, they don't appear in searches, it leaves all of their stuff (forum posts, wiki pages, etc.) referencing existing user records, ...
AB Bader, modified 8 Years ago.

RE: delete liferay user programmatically

Junior Member Posts: 47 Join Date: 8/5/14 Recent Posts
ok but in my java code when i need to get all users with List<User> listuser=UserLocalServiceUtil.getUsers(0, UserLocalServiceUtil.getUsersCount());
this list contains active and inactive users ! So how can I have just the list of active users ?

thank you
thumbnail
David H Nebinger, modified 8 Years ago.

RE: delete liferay user programmatically

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
use the search methods instead as you can include the status column, also use QueryUtil.ALL_POS so you don't have to query for the count.
AB Bader, modified 8 Years ago.

RE: delete liferay user programmatically

Junior Member Posts: 47 Join Date: 8/5/14 Recent Posts
thank you it's works emoticon
My code :


DynamicQuery dynamicQuery = UserLocalServiceUtil.dynamicQuery();
dynamicQuery.add(RestrictionsFactoryUtil.ne("status", 5));
int startOfList = QueryUtil.ALL_POS;
int endOfList = QueryUtil.ALL_POS;
List<user> listuser=UserLocalServiceUtil.dynamicQuery(dynamicQuery,QueryUtil.ALL_POS, QueryUtil.ALL_POS);
</user>
David George, modified 4 Years ago.

RE: delete liferay user programmatically

New Member Posts: 7 Join Date: 4/8/11 Recent Posts
asto d:

It throws this exception :
com.liferay.portal.NoSuchGroupException: No Group exists with the key {companyId=10154, classNameId=10190, classPK=100601}

This probably means you deleted the Default User, you can check for this in your code user.isDefaulUser() (returns true/false).
Old post I know but in case anyone else is looking  at this.
Micael Ericsson, modified 3 Years ago.

RE: delete liferay user programmatically

Regular Member Posts: 169 Join Date: 12/11/12 Recent Posts
David George:

asto d:

It throws this exception :
com.liferay.portal.NoSuchGroupException: No Group exists with the key {companyId=10154, classNameId=10190, classPK=100601}

This probably means you deleted the Default User, you can check for this in your code user.isDefaulUser() (returns true/false).
Old post I know but in case anyone else is looking  at this.
God post. Almost our problem. I guess that default user has been deleted and with it the group matching these 3 keys. But now we have a new default user with no matching group. How do I recreate matching group? Or can I create a new default user and group is automatically created?
 
Taruchit Goyal, modified 5 Years ago.

RE: delete liferay user programmatically

Junior Member Posts: 60 Join Date: 3/16/18 Recent Posts

Hello,

I am trying to write a Groovy script which helps to delete users based on user id given in the if condition.

import com.liferay.portal.kernel.service.UserLocalServiceUtil
try{
userCount = UserLocalServiceUtil.getUsersCount()
users = UserLocalServiceUtil.getUsers(0, userCount)

for (user in users) {

    if(user.getUserId()==36615){
    
 println("User Name: " + user.getFullName() + " -- " +
user.getAgreedToTermsOfUse()+" Email address: "+user.getEmailAddress()+" User ID: "+user.getUserId());
UserLocalServiceUtil.deleteUser(user);
}}
}catch(e){
 out.println("""<div class="portlet-msg-error">${e}</div>""")
        e.printStackTrace(out)
}

But, it gives "com.liferay.portal.kernel.exception.ModelListenerException: java.lang.NullPointerException"
Please help to resolve the same.

 

Thanks
Taruchit

thumbnail
David H Nebinger, modified 5 Years ago.

RE: delete liferay user programmatically

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts

You'd have to provide the stack trace.  Obviously the model listener is expecting something to be there that isn't, but it isn't clear if this is a Liferay model listener or a home-grown model listener.

thumbnail
Alberto Chaparro, modified 5 Years ago.

RE: delete liferay user programmatically

Liferay Master Posts: 549 Join Date: 4/25/11 Recent Posts

Hey David,

 

The stack trace is here:

https://community.liferay.com/forums/-/message_boards/view_message/110981462

 

 

However I don't know why Taruchit has not opened a new thread? Taruchit, don't you permissions to open a new thread?

 

Regarding the issue it seems that the exception is thrown in a Social Office portlet:

at com.liferay.social.office.upgrade.association.internal.model.listener.UserModelListener.onAfterRemoveAssociation(UserModelListener.java:193)

 

If you don't use it, could you uninstall social office and execute the script again?

Taruchit Goyal, modified 5 Years ago.

RE: delete liferay user programmatically

Junior Member Posts: 60 Join Date: 3/16/18 Recent Posts

Hello David,

Following is the stack trace for the issue: -

https://community.liferay.com/de/forums/-/message_boards/message/111015368

 

Taruchit Goyal

james houston, modified 5 Years ago.

RE: delete liferay user programmatically

New Member Post: 1 Join Date: 10/8/18 Recent Posts

HI i need a developer to do some work on eCommerce checkout page can any one help me with  their right charges.